Chinaunix首页 | 论坛 | 博客
  • 博客访问: 502064
  • 博文数量: 153
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 1724
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-08 11:55
文章分类

全部博文(153)

文章存档

2011年(1)

2010年(55)

2009年(88)

2008年(9)

我的朋友

分类: LINUX

2010-10-26 17:32:56



  1. Service, Broadcast, BroadcastReceiver 的演示  
  2. Main.java  
  3.   
  4. 代 码   
  5. package com.webabcd.service;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.BroadcastReceiver;  
  9. import android.content.ComponentName;  
  10. import android.content.Context;  
  11. import android.content.Intent;  
  12. import android.content.IntentFilter;  
  13. import android.content.ServiceConnection;  
  14. import android.os.Bundle;  
  15. import android.os.IBinder;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.widget.TextView;  
  19.   
  20. /* 
  21.  * startService() 和 bindService() 的 区别  
  22.  * startService() - 正 常理解就好 
  23.  * bindService() - 使 当前上下文对象(本例中就是 Activity)通过一个 ServiceConnection 对象邦定到指定的 Service 。这样,如果上下文 对象销毁了的话,那么其对应的 Service 也会被销毁 
  24.  */  
  25. public class Main extends Activity implements OnClickListener {  
  26.   
  27.     private TextView txtMsg;  
  28.       
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.   
  34.         setTitle("android 之 service");  
  35.   
  36.         this.findViewById(R.id.btnStart).setOnClickListener(this);  
  37.         this.findViewById(R.id.btnStop).setOnClickListener(this);  
  38.         this.findViewById(R.id.btnBind).setOnClickListener(this);  
  39.         this.findViewById(R.id.btnUnbind).setOnClickListener(this);  
  40.           
  41.         txtMsg = (TextView)this.findViewById(R.id.txtMsg);  
  42.           
  43.         // 实例化自定义的 BroadcastReceiver  
  44.         receiver = new UpdateReceiver();  
  45.         IntentFilter filter = new IntentFilter();  
  46.         // 为 BroadcastReceiver 指定 action ,使之用于接收同 action 的广播  
  47.         filter.addAction("com.webabcd.service.msg");  
  48.           
  49.         // 以编程方式注册  BroadcastReceiver 。配置方式注 册 BroadcastReceiver 的例子见 AndroidManifest.xml 文件  
  50.         // 一般在 OnStart 时注册,在 OnStop 时取消注册  
  51.         this.registerReceiver(receiver, filter);  
  52.         // this.unregisterReceiver(receiver);  
  53.           
  54.     }  
  55.   
  56.     @Override  
  57.     public void onClick(View v) {  
  58.         Intent intent = new Intent(Main.this, MyService.class);  
  59.         switch (v.getId()) {  
  60.         case R.id.btnStart:  
  61.             this.startService(intent);  
  62.             break;  
  63.         case R.id.btnStop:  
  64.             this.stopService(intent);  
  65.             break;  
  66.         case R.id.btnBind:  
  67.             this.bindService(intent, conn, Context.BIND_AUTO_CREATE);  
  68.             break;  
  69.         case R.id.btnUnbind:  
  70.             this.unbindService(conn);  
  71.             break;  
  72.         }  
  73.     }  
  74.   
  75.     // bindService() 所需的 ServiceConnection 对象  
  76.     private ServiceConnection conn = new ServiceConnection() {  
  77.         @Override  
  78.         public void onServiceConnected(ComponentName className, IBinder service) {  
  79.               
  80.         }  
  81.         @Override  
  82.         public void onServiceDisconnected(ComponentName className) {  
  83.               
  84.         }  
  85.     };  
  86.       
  87.     private String msg="";  
  88.     private UpdateReceiver receiver;  
  89.     // 实现一个 BroadcastReceiver,用于接收指定的 Broadcast  
  90.     public class UpdateReceiver extends BroadcastReceiver{  
  91.   
  92.         @Override  
  93.         public void onReceive(Context context, Intent intent) {  
  94.             msg = intent.getStringExtra("msg");  
  95.               
  96.             txtMsg.append(msg + "\n");  
  97.         }  
  98.           
  99.     }  
  100. }  
  101.   
  102. MyService.java  
  103.   
  104. 代 码   
  105. package com.webabcd.service;  
  106.   
  107. import android.app.Service;  
  108. import android.content.Intent;  
  109. import android.os.IBinder;  
  110. import android.util.Log;  
  111.   
  112. // 演示 Service 的生命周期。具体信息运行程序后在 LogCat 中查看  
  113. public class MyService extends Service {  
  114.   
  115.     @Override  
  116.     public IBinder onBind(Intent intent) {  
  117.           
  118.         Log.d("MyDebug""onBind");  
  119.         sendMsg("onBind");  
  120.           
  121.         // TODO Auto-generated method stub  
  122.         return null;  
  123.     }  
  124.   
  125.     @Override  
  126.     public void onCreate() {  
  127.         // TODO Auto-generated method stub  
  128.         super.onCreate();  
  129.           
  130.         Log.d("MyDebug""onCreate");  
  131.         sendMsg("onCreate");  
  132.     }  
  133.   
  134.     @Override  
  135.     public void onDestroy() {  
  136.         // TODO Auto-generated method stub  
  137.         super.onDestroy();  
  138.           
  139.         Log.d("MyDebug""onDestroy");  
  140.         sendMsg("onDestroy");  
  141.     }  
  142.   
  143.     @Override  
  144.     public void onRebind(Intent intent) {  
  145.         // TODO Auto-generated method stub  
  146.         super.onRebind(intent);  
  147.           
  148.         Log.d("MyDebug""onRebind");  
  149.         sendMsg("onRebind");  
  150.     }  
  151.   
  152.     @Override  
  153.     public void onStart(Intent intent, int startId) {  
  154.         super.onStart(intent, startId);  
  155.           
  156.         Log.d("MyDebug""onStart");  
  157.         sendMsg("onStart");  
  158.     }  
  159.       
  160.     @Override  
  161.     public boolean onUnbind(Intent intent) {  
  162.           
  163.         Log.d("MyDebug""onUnbind");  
  164.         sendMsg("onUnbind");  
  165.           
  166.         // TODO Auto-generated method stub  
  167.         return super.onUnbind(intent);  
  168.     }  
  169.       
  170.     // 发送广播信息  
  171.     private void sendMsg(String msg){  
  172.         // 指定广播目标的 action (注:指定了此 action 的 receiver 会接收此广播)  
  173.         Intent intent = new Intent("com.webabcd.service.msg");  
  174.         // 需要传递的参数  
  175.         intent.putExtra("msg", msg);  
  176.         // 发送广播  
  177.         this.sendBroadcast(intent);  
  178.     }  
  179. }  
  180.   
  181.   
  182. MyBootReceiver.java  
  183.   
  184. 代 码   
  185. package com.webabcd.service;  
  186.   
  187. import android.content.BroadcastReceiver;  
  188. import android.content.Context;  
  189. import android.content.Intent;  
  190. import android.util.Log;  
  191.   
  192. public class MyBootReceiver extends BroadcastReceiver {  
  193.   
  194.     // 用于接收满足条件的 Broadcast(相应的 Broadcast 的注册信息详 见 AndroidManifest.xml ,当系统启动完毕后会调用这个广播接收器)  
  195.     @Override  
  196.     public void onReceive(Context arg0, Intent arg1) {  
  197.         Log.d("MyDebug""onReceive");  
  198.           
  199.         // 启动服务  
  200.         Intent service = new Intent(arg0, MyService.class);  
  201.         arg0.startService(service);  
  202.     }  
  203.   
  204. }  
  205.   
  206.   
  207. AndroidManifest.xml  
  208.   
  209. 代 码   
  210. "1.0" encoding="utf-8"?>  
  211. ""  
  212.     package="com.webabcd.service" android:versionCode="1"  
  213.     android:versionName="1.0">  
  214.     "@drawable/icon" android:label="@string/app_name">  
  215.         ".Main" android:label="@string/app_name">  
  216.               
  217.                 "android.intent.action.MAIN" />  
  218.                 "android.intent.category.LAUNCHER" />  
  219.               
  220.           
  221.           
  222.           
  223.         ".MyService">  
  224.           
  225.           
  226.         ".MyBootReceiver">  
  227.               
  228.                 "android.intent.action.BOOT_COMPLETED" />  
  229.               
  230.           
  231.       
  232.       
  233.       
  234.     "android.permission.RECEIVE_BOOT_COMPLETED" />  
  235.     "3" />  
  236.   

阅读(953) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~