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