免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1664 | 回复: 1
打印 上一主题 下一主题

Android 实现一个Service应用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-04 16:17 |只看该作者 |倒序浏览
Android 实现一个Service应用












紧接着前面的那篇博客继续加上代码实现一个service
利用eclipse制作界面新版的ADT支持直接拖动,很方便的设计自己的UI




首先定义一个Activity
Java代码
  1. package com.houyewei.myservice;   
  2.   
  3. import android.app.Activity;   
  4. import android.app.Service;   
  5. import android.content.ComponentName;   
  6. import android.content.Intent;   
  7. import android.content.ServiceConnection;   
  8. import android.os.Bundle;   
  9. import android.os.IBinder;   
  10. import android.util.Log;   
  11. import android.view.View;   
  12. import android.view.View.OnClickListener;   
  13. import android.widget.Button;   
  14. import android.widget.Toast;   
  15.   
  16. /*  
  17. * @author:Wynston Hou  
  18. * @email :   
  19. */  
  20.   
  21. public class MainActivity extends Activity {   
  22.     private Button startBtn, stopBtn, bindBtn, unbindBtn;   
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {   
  26.         super.onCreate(savedInstanceState);   
  27.         // 设置Activity的界面的布局   
  28.         setContentView(R.layout.main);   
  29.         // 通过findViewByid   
  30.         startBtn = (Button) findViewById(R.id.startButton01);   
  31.         stopBtn = (Button) findViewById(R.id.stopButton02);   
  32.         bindBtn = (Button) findViewById(R.id.bindButton03);   
  33.         unbindBtn = (Button) findViewById(R.id.unbindButton04);   
  34.         // 添加监听   
  35.         startBtn.setOnClickListener(startlistener);   
  36.         stopBtn.setOnClickListener(stoplistener);   
  37.         bindBtn.setOnClickListener(bindlistener);   
  38.         unbindBtn.setOnClickListener(unbindlistener);   
  39.   
  40.     };   
  41.   
  42.     private OnClickListener startlistener = new OnClickListener() {   
  43.         @Override  
  44.         public void onClick(View v) {   
  45.             Intent intent = new Intent();   
  46.             intent.setAction("com.houyewei.myservice.action.MY_SERVICE");   
  47.             startService(intent);   
  48.   
  49.         }   
  50.     };   
  51.     private OnClickListener stoplistener = new OnClickListener() {   
  52.         @Override  
  53.         public void onClick(View v) {   
  54.             Intent intent = new Intent();   
  55.             intent.setAction("com.houyewei.myservice.action.MY_SERVICE");   
  56.             stopService(intent);   
  57.         }   
  58.     };   
  59.   
  60.     private ServiceConnection conn = new ServiceConnection() {   
  61.         @Override  
  62.         public void onServiceConnected(ComponentName name, IBinder service) {   
  63.             Log.i("SERVICE", "连接成功");   
  64.             Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();   
  65.   
  66.         }   
  67.   
  68.         @Override  
  69.         public void onServiceDisconnected(ComponentName name) {   
  70.             // TODO Auto-generated method stub   
  71.   
  72.         }   
  73.   
  74.     };   
  75.   
  76.     private OnClickListener bindlistener = new OnClickListener() {   
  77.         @Override  
  78.         public void onClick(View v) {   
  79.             Intent intent = new Intent();   
  80.             intent.setAction("com.houyewei.myservice.action.MY_SERVICE");   
  81.             bindService(intent, conn, Service.BIND_AUTO_CREATE);   
  82.   
  83.         }   
  84.     };   
  85.     private OnClickListener unbindlistener = new OnClickListener() {   
  86.         public void onClick(View v) {   
  87.             Intent intent = new Intent();   
  88.             intent.setAction("com.houyewei.myservice.action.MY_SERVICE");   
  89.             unbindService(conn);   
  90.   
  91.         }   
  92.     };   
  93.   
  94. }  

  95. package com.houyewei.myservice;

  96. import android.app.Activity;
  97. import android.app.Service;
  98. import android.content.ComponentName;
  99. import android.content.Intent;
  100. import android.content.ServiceConnection;
  101. import android.os.Bundle;
  102. import android.os.IBinder;
  103. import android.util.Log;
  104. import android.view.View;
  105. import android.view.View.OnClickListener;
  106. import android.widget.Button;
  107. import android.widget.Toast;

  108. /*
  109. * @author:Wynston Hou
  110. * @email :
  111. */

  112. public class MainActivity extends Activity {
  113.         private Button startBtn, stopBtn, bindBtn, unbindBtn;

  114.         @Override
  115.         public void onCreate(Bundle savedInstanceState) {
  116.                 super.onCreate(savedInstanceState);
  117.                 // 设置Activity的界面的布局
  118.                 setContentView(R.layout.main);
  119.                 // 通过findViewByid
  120.                 startBtn = (Button) findViewById(R.id.startButton01);
  121.                 stopBtn = (Button) findViewById(R.id.stopButton02);
  122.                 bindBtn = (Button) findViewById(R.id.bindButton03);
  123.                 unbindBtn = (Button) findViewById(R.id.unbindButton04);
  124.                 // 添加监听
  125.                 startBtn.setOnClickListener(startlistener);
  126.                 stopBtn.setOnClickListener(stoplistener);
  127.                 bindBtn.setOnClickListener(bindlistener);
  128.                 unbindBtn.setOnClickListener(unbindlistener);

  129.         };

  130.         private OnClickListener startlistener = new OnClickListener() {
  131.                 @Override
  132.                 public void onClick(View v) {
  133.                         Intent intent = new Intent();
  134.                         intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
  135.                         startService(intent);

  136.                 }
  137.         };
  138.         private OnClickListener stoplistener = new OnClickListener() {
  139.                 @Override
  140.                 public void onClick(View v) {
  141.                         Intent intent = new Intent();
  142.                         intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
  143.                         stopService(intent);
  144.                 }
  145.         };

  146.         private ServiceConnection conn = new ServiceConnection() {
  147.                 @Override
  148.                 public void onServiceConnected(ComponentName name, IBinder service) {
  149.                         Log.i("SERVICE", "连接成功");
  150.                         Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();

  151.                 }

  152.                 @Override
  153.                 public void onServiceDisconnected(ComponentName name) {
  154.                         // TODO Auto-generated method stub

  155.                 }

  156.         };

  157.         private OnClickListener bindlistener = new OnClickListener() {
  158.                 @Override
  159.                 public void onClick(View v) {
  160.                         Intent intent = new Intent();
  161.                         intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
  162.                         bindService(intent, conn, Service.BIND_AUTO_CREATE);

  163.                 }
  164.         };
  165.         private OnClickListener unbindlistener = new OnClickListener() {
  166.                 public void onClick(View v) {
  167.                         Intent intent = new Intent();
  168.                         intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
  169.                         unbindService(conn);

  170.                 }
  171.         };

  172. }
复制代码
一下是service中的内容
Java代码
  1. package com.houyewei.myservice;   
  2.   
  3. import android.app.Service;   
  4. import android.content.Intent;   
  5. import android.os.IBinder;   
  6. import android.util.Log;   
  7. import android.widget.Toast;   
  8.   
  9. public class MyService extends Service {   
  10.     public IBinder onBind(Intent intent) {   
  11.         Log.i("SERVICE", "onBInd...................");   
  12.         Toast.makeText(MyService.this, "onBInd.........", Toast.LENGTH_LONG)   
  13.                 .show();   
  14.         return null;   
  15.   
  16.     }   
  17.   
  18.     public void onCreate() {   
  19.         Log.i("SERVICE", "onCreate........");   
  20.         Toast.makeText(MyService.this, "onCreatehaha ........",   
  21.                 Toast.LENGTH_LONG).show();   
  22.   
  23.     }   
  24.   
  25.     public void onStart(Intent intent, int startId) {   
  26.         Log.i("SERVICE", "onStartoing..............");   
  27.         Toast.makeText(MyService.this, "onStart....哈哈", Toast.LENGTH_LONG)   
  28.                 .show();   
  29.     }   
  30.   
  31.     public void onDestory() {   
  32.         Log.i("SERVICE", "Ondestory.........");   
  33.         Toast.makeText(MyService.this, "OnDestory。。。。。hhaha", Toast.LENGTH_LONG)   
  34.                 .show();   
  35.   
  36.     }   
  37.   
  38. }  

  39. package com.houyewei.myservice;

  40. import android.app.Service;
  41. import android.content.Intent;
  42. import android.os.IBinder;
  43. import android.util.Log;
  44. import android.widget.Toast;

  45. public class MyService extends Service {
  46.         public IBinder onBind(Intent intent) {
  47.                 Log.i("SERVICE", "onBInd...................");
  48.                 Toast.makeText(MyService.this, "onBInd.........", Toast.LENGTH_LONG)
  49.                                 .show();
  50.                 return null;

  51.         }

  52.         public void onCreate() {
  53.                 Log.i("SERVICE", "onCreate........");
  54.                 Toast.makeText(MyService.this, "onCreatehaha ........",
  55.                                 Toast.LENGTH_LONG).show();

  56.         }

  57.         public void onStart(Intent intent, int startId) {
  58.                 Log.i("SERVICE", "onStartoing..............");
  59.                 Toast.makeText(MyService.this, "onStart....哈哈", Toast.LENGTH_LONG)
  60.                                 .show();
  61.         }

  62.         public void onDestory() {
  63.                 Log.i("SERVICE", "Ondestory.........");
  64.                 Toast.makeText(MyService.this, "OnDestory。。。。。hhaha", Toast.LENGTH_LONG)
  65.                                 .show();

  66.         }

  67. }
复制代码
在AndroidManifest中定义Service 和Activity


运行结果



3.jpg (54.4 KB, 下载次数: 1)

3.jpg

3.jpg (54.4 KB, 下载次数: 0)

3.jpg

3.jpg (54.4 KB, 下载次数: 1)

3.jpg

3.jpg (54.4 KB, 下载次数: 0)

3.jpg

3.jpg (54.4 KB, 下载次数: 2)

3.jpg

3.jpg (54.4 KB, 下载次数: 0)

3.jpg

论坛徽章:
0
2 [报告]
发表于 2011-12-23 23:23 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP