android 广播 android Service 开机启动的Service

来源:互联网 发布:c语言结构体使用 编辑:程序博客网 时间:2024/05/06 10:55

android 广播 android Service 开机启动的Service




  1. package com.webabcd.service;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.ComponentName;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.IntentFilter;  
  9. import android.content.ServiceConnection;  
  10. import android.os.Bundle;  
  11. import android.os.IBinder;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.TextView;  
  15.   
  16. /* 
  17.  * startService() 和 bindService() 的区别  
  18.  * startService() - 正常理解就好 
  19.  * bindService() - 使当前上下文对象(本例中就是 Activity)通过一个 ServiceConnection 对象邦定到指定的 Service 。这样,如果上下文对象销毁了的话,那么其对应的 Service 也会被销毁 
  20.  */  
  21. public class Main extends Activity implements OnClickListener {  
  22.   
  23.     private TextView txtMsg;  
  24.         
  25.     @Override    
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.   
  30.         setTitle("android 之 service");  
  31.   
  32.         this.findViewById(R.id.btnStart).setOnClickListener(this);  
  33.         this.findViewById(R.id.btnStop).setOnClickListener(this);  
  34.         this.findViewById(R.id.btnBind).setOnClickListener(this);  
  35.         this.findViewById(R.id.btnUnbind).setOnClickListener(this);  
  36.           
  37.         txtMsg = (TextView)this.findViewById(R.id.txtMsg);  
  38.           
  39.         // 实例化自定义的 BroadcastReceiver  
  40.         receiver = new UpdateReceiver();  
  41.         IntentFilter filter = new IntentFilter();  
  42.         // 为 BroadcastReceiver 指定 action ,使之用于接收同 action 的广播  
  43.         filter.addAction("com.webabcd.service.msg");  
  44.           
  45.         // 以编程方式注册  BroadcastReceiver 。就是在代码中注册  
  46.         //配置方式注册 BroadcastReceiver 的例子见 AndroidManifest.xml 文件  
  47.         // 一般在 OnStart 时注册,在 OnStop 时取消注册  
  48.         this.registerReceiver(receiver, filter);  
  49.         // this.unregisterReceiver(receiver);  
  50.           
  51.     }  
  52.   
  53.     @Override  
  54.     public void onClick(View v) {  
  55.         Intent intent = new Intent(Main.this, MyService.class);  
  56.         switch (v.getId()) {  
  57.         case R.id.btnStart:  
  58.             this.startService(intent);  
  59.             break;  
  60.         case R.id.btnStop:  
  61.             this.stopService(intent);  
  62.             break;  
  63.         case R.id.btnBind:  
  64.             this.bindService(intent, conn, Context.BIND_AUTO_CREATE);  
  65.             break;  
  66.         case R.id.btnUnbind:  
  67.             this.unbindService(conn);  
  68.             break;  
  69.         }  
  70.     }  
  71.   
  72.     // bindService() 所需的 ServiceConnection 对象  
  73.     private ServiceConnection conn = new ServiceConnection() {  
  74.         @Override  
  75.         public void onServiceConnected(ComponentName className, IBinder service) {  
  76.               
  77.         }  
  78.         @Override  
  79.         public void onServiceDisconnected(ComponentName className) {  
  80.               
  81.         }  
  82.     };  
  83.       
  84.     private String msg="";  
  85.     private UpdateReceiver receiver;  
  86.     // 实现一个 BroadcastReceiver,用于接收指定的 Broadcast  
  87.     public class UpdateReceiver extends BroadcastReceiver{  
  88.   
  89.         @Override  
  90.         public void onReceive(Context context, Intent intent) {  
  91.             msg = intent.getStringExtra("msg");  
  92.             txtMsg.append(msg + "/n");  
  93.         }  
  94.           
  95.     }  
  96. }  

 

 

view plain
  1. package com.webabcd.service;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. public class MyBootReceiver extends BroadcastReceiver {  
  9.   
  10.     // 用于接收满足条件的 Broadcast(相应的 Broadcast 的注册信息详见 AndroidManifest.xml ,当系统启动完毕后会调用这个广播接收器)  
  11.     //当开机的时候就会启动这个Service  
  12.     @Override  
  13.     public void onReceive(Context arg0, Intent arg1) {  
  14.         Log.d("MyDebug""onReceive");  
  15.         // 启动服务  
  16.         Intent service = new Intent(arg0, MyService.class);  
  17.         arg0.startService(service);  
  18.     }  
  19.   
  20. }  
view plain
  1. package com.webabcd.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6. import android.util.Log;  
  7.   
  8. // 演示 Service 的生命周期。具体信息运行程序后在 LogCat 中查看  
  9. public class MyService extends Service {  
  10.   
  11.     @Override  
  12.     public IBinder onBind(Intent intent) {  
  13.           
  14.         Log.d("MyDebug""onBind");  
  15.         sendMsg("onBind");  
  16.           
  17.         // TODO Auto-generated method stub  
  18.         return null;  
  19.     }  
  20.   
  21.     @Override  
  22.     public void onCreate() {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate();  
  25.           
  26.         Log.d("MyDebug""onCreate");  
  27.         sendMsg("onCreate");  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onDestroy() {  
  32.         // TODO Auto-generated method stub  
  33.         super.onDestroy();  
  34.           
  35.         Log.d("MyDebug""onDestroy");  
  36.         sendMsg("onDestroy");  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onRebind(Intent intent) {  
  41.         // TODO Auto-generated method stub  
  42.         super.onRebind(intent);  
  43.           
  44.         Log.d("MyDebug""onRebind");  
  45.         sendMsg("onRebind");  
  46.     }  
  47.   
  48.     @Override  
  49.     public void onStart(Intent intent, int startId) {  
  50.         super.onStart(intent, startId);  
  51.           
  52.         Log.d("MyDebug""onStart");  
  53.         sendMsg("onStart");  
  54.     }  
  55.       
  56.     @Override  
  57.     public boolean onUnbind(Intent intent) {  
  58.           
  59.         Log.d("MyDebug""onUnbind");  
  60.         sendMsg("onUnbind");  
  61.           
  62.         // TODO Auto-generated method stub  
  63.         return super.onUnbind(intent);  
  64.     }  
  65.       
  66.     // 发送广播信息  
  67.     private void sendMsg(String msg){  
  68.         // 指定广播目标的 action (注:指定了此 action 的 receiver 会接收此广播)  
  69.         Intent intent = new Intent("com.webabcd.service.msg");  
  70.         // 需要传递的参数  
  71.         intent.putExtra("msg", msg);  
  72.         // 发送广播  
  73.         this.sendBroadcast(intent);  
  74.     }  
  75. }  

大家有什么疑问可以留言,我们共同解决!留言哦!

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.webabcd.service" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:name=".Main" android:label="@string/app_name">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.         </activity>  
  12.           
  13.         <!-- 
  14.             如果有需要用到的 service ,则都要在这里做相应的配置 
  15.         -->  
  16.         <service android:name=".MyService"></service>  
  17.           
  18.         <!--  
  19.             注册一个 BroadcastReceiver  
  20.             其 intent-filter 为 android.intent.action.BOOT_COMPLETED(用于接收系统启动完毕的 Broadcast)  
  21.         -->  
  22.         <receiver android:name=".MyBootReceiver">  
  23.             <intent-filter>  
  24.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  
  25.             </intent-filter>  
  26.         </receiver>  
  27.     </application>  
  28.       
  29.     <!-- 
  30.         接受系统启动完毕的 Broadcast 的权限 
  31.     -->  
  32.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  
  33.     <uses-sdk android:minSdkVersion="3" />  
  34. </manifest>   

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 和老婆吵架她跑回娘家了怎么办 鞋子里面的皮穿的都是泥怎么办 绝地求生手游模拟器键位不灵怎么办 我更新目录的时候目录不存在怎么办 如果别人欠我钱不还 我该怎么办 老婆欠信用卡的钱我该怎么办 我欠了很多钱我该怎么办 一个人欠我钱跑了我该怎么办 买房子时间长了成危楼了怎么办 别人欠我钱人找不到了怎么办 欠银行信用卡钱人失踪了怎么办 欧洲卡车模拟2没油了怎么办 蓝牙安装包里没有微信怎么办 腾讯手游模拟器玩全军出击卡怎么办 手游cf用模拟器画面很抖怎么办 腾讯手游助手模拟器游戏卡死怎么办 腾讯手游模拟器提示注册以满怎么办 手游绝地求生被检测成模拟器怎么办 腾讯手游助手模拟器内存不够怎么办 微信视频对方听不到我的声音怎么办 学生考试传纸条作弊老师应怎么办 苹果手机微信小游戏没有声音怎么办 红米手机游戏下好了安装不了怎么办 小米手机sd卡存储已满怎么办 英雄联盟峡谷之巅资格被收回怎么办 人进监狱之前信用卡没还怎么办 荒野行动资源文件下载卡住了怎么办 荒野行动绑定的手机号停机了怎么办 不小心误点了爱奇艺扣款了怎么办 和别人吵架别人先骂你怎么办 自己人被带到了缅甸黑社会了怎么办 绝地求生忘记复制钥匙激活码怎么办 绝地求生买的钥匙激活码丢了怎么办 蓝河奶粉宝宝吃了不长肉怎么办 苹果ios版本太低激活不了怎么办 脸上被油烫伤了 起了水泡怎么办 皮肤被油烫伤起来个水泡怎么办 去泰国旅游没来得及兑换泰铢怎么办 游戏和安卓 不和 出现黑屏怎么办 邻居把垃圾放在楼梯口不丢怎么办 58热敏小票打印机口松了怎么办