android service and broadcast
来源:互联网 发布:ipad版的淘宝微淘在哪 编辑:程序博客网 时间:2024/05/21 04:16
1. MyService
public class MyService extends Service {private final static String TAG = "main";public final static String SEND_OK_MESSAGE = "send.ok.message"; public final static String SEND_CANCLE_MESSAGE = "send.cancle.message";private Context mContext;private int count;private MyBinder binder=new MyBinder();private Thread thread;private boolean quit;@Overridepublic IBinder onBind(Intent arg0) {if(mContext!=null){Toast.makeText(mContext, "绑定成功", Toast.LENGTH_LONG).show();}return binder;} public boolean onUnbind(Intent intent) {if(mContext!=null){Toast.makeText(mContext, "解除绑定", Toast.LENGTH_LONG).show();}return true;}public void onCreate(){super.onCreate();thread=new Thread(new Runnable() {@Overridepublic void run() {while(!quit){try{Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}count++;}} });thread.start();} //启动服务时,注册一个广播public void onStart(Intent intent, int startId) {IntentFilter myFilter = new IntentFilter(); myFilter.addAction(SEND_OK_MESSAGE); myFilter.addAction(SEND_CANCLE_MESSAGE); this.registerReceiver(myBroadCast, myFilter); super.onStart(intent, startId); }private BroadcastReceiver myBroadCast = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {//广播接收到消息mContext = context;String action = intent.getAction(); if (action.equals(SEND_OK_MESSAGE)) { Toast.makeText(context, "接收到了一条广播为" + SEND_OK_MESSAGE, Toast.LENGTH_LONG).show(); }else if(action.equals(SEND_CANCLE_MESSAGE)) { Toast.makeText(context, "接收到了一条广播为" + SEND_CANCLE_MESSAGE, Toast.LENGTH_LONG).show(); }} };class MyBinder extends Binder{ public int getCount(){return count;}}}
2. MyBootReceiver
public class MyBootReceiver extends BroadcastReceiver {static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; @Overridepublic void onReceive(Context context, Intent intent) {//扩展一个广播类,用于开机就启动服务if (intent.getAction().equals(BOOT_COMPLETED)) {Log.i("MyBootReceiver", "yzf, onReceive, startService");Intent i = new Intent(context, MyService.class);context.startService(i); }} }
3. MainActivity
public class MainActivity extends Activity implements OnClickListener {private Button btnOk;private Button btnCancel;private Intent intent; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i("MainActivity", "yzf, onCreate");btnOk = (Button) findViewById(R.id.btn_ok);btnCancel = (Button) findViewById(R.id.btn_cancel);btnOk.setOnClickListener(this);btnCancel.setOnClickListener(this);//启动服务intent = new Intent(this, MyService.class); startService(intent);}//用以发送广播public void onClick(View arg0) {// TODO Auto-generated method stubIntent vIntent;switch(arg0.getId()){case R.id.btn_ok:vIntent = new Intent("send.ok.message");sendBroadcast(vIntent); break;case R.id.btn_cancel:vIntent = new Intent("send.cancle.message");sendBroadcast(vIntent);break;}} public void onDestroy(){ super.onDestroy(); if(intent != null){ stopService(intent); } } }
4. 服务与广播的XML注册
<service android:name=".MyService"> <intent-filter> <action android:name="cn.m15.xys.MyService"></action> </intent-filter> <intent-filter> <action android:name="send.ok.message" /> <action android:name="send.cancle.message" /> </intent-filter> </service> <receiver android:name=".MyBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
0 0
- android service and broadcast
- Android Service and Broadcast Receiver Tutorial
- android service broadcast notification
- android的Broadcast Service
- Android Broadcast and BroadcastReceiver
- Android Broadcast Intents and Broadcast Receivers
- Android Broadcast Intents and Broadcast Receivers
- broadcast Service
- Android Activity& service&Broadcast receiver 生命周期
- android中service、activity、broadcast的使用
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android service、broadcast后台启动Activity
- android插件化(BroadCast、Service篇)
- jQuery 自定义网页滚动条样式插件 mCustomScrollbar 的介绍和使用方法
- 解决ORA-03113: 通信通道的文件结尾(实质是Control filesequence number mismatch)
- UVA - 11795 Mega Man's Mission 状态压缩DP
- Android Fragment 实战(一)
- 《编程珠玑》开篇题解析,session问题,日志总结
- android service and broadcast
- Oracle 删除重复数据
- 代码规范
- java中HashMap详解
- [windows phone开发]导航(传参)
- windows系统 ---系统文件篇
- Fuse文件系统优化方案
- 如何使用UIAutomation进行iOS 自动化测试(Part I)
- C#随机数、重复几率小