Android 里的各类通讯方式(四)

五:广播android

Android开发中若是须要对两个彻底不要紧的程序之间进行通讯就可使用发送广播与接收广播的机制来实现 ,例如程序A发送了一个广播 程序B接受到 作一些事情 这样就达到了相互的通信。 ide

public class BroadcastActivity extends Activity {    this

    Button mButton0 = null;    code

    Button mButton1 = null;    xml

    @Override    开发

    protected void onCreate(Bundle savedInstanceState) {    get

    setContentView(R.layout.broadcast);       it

    mButton0 = (Button)findViewById(R.id.button0);    io

    mButton0.setOnClickListener(new OnClickListener() {       ast

        @Override    

        public void onClick(View arg0) {    

                Intent intent = new Intent(MyService.SEND_OK_MESSAGE);    

                intent.putExtra("name", "您发送了OK这条广播");    

                sendBroadcast(intent);    

        }    

    });    

    mButton1 = (Button)findViewById(R.id.button1);    

    mButton1.setOnClickListener(new OnClickListener() {      

        @Override    

        public void onClick(View arg0) {    

                Intent intent = new Intent(MyService.SEND_CANCLE_MESSAGE);    

                intent.putExtra("name", "您发送了Cancle这条广播哦");    

                sendBroadcast(intent);    

        }    

    });    

    //启动Service     

    Intent i = new Intent(this, MyService.class);    

    startService(i);    

    super.onCreate(savedInstanceState);    

    }    

}    

接收广播的话 咱们开启一个service 在service中经过BroadcastReceiver 来接收广播 前提是需要接收的广播需要在onStart()中注册一下 在AndroidManifest.xml中能够过滤只接收需要接收的广播、 

<service android:name=".MyService">    

    <intent-filter>    

        <action android:name="cn.boweifeng.abc.MyService"></action>    

    </intent-filter>    

    <intent-filter>    

        <action android:name="send.ok.message" />    

        <action android:name="send.cancle.message" />    

    </intent-filter>    

</service>    

在onStart()中注册了程序中所须要的两个广播  

public class MyService extends Service {    

    public final static String SEND_OK_MESSAGE = "send.ok.message";    

    public final static String SEND_CANCLE_MESSAGE = "send.cancle.message";    

    private BroadcastReceiver myBroadCast = new BroadcastReceiver() {    

    @Override    

    public void onReceive(Context context, Intent intent) {    

        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();    

        }    

    }     

    };     

    @Override    

    public void onCreate() {    

    super.onCreate();    

    }     

    @Override    

    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);    

    }    

    @Override    

    public IBinder onBind(Intent arg0) {    

    return null;    

    }     

}     

        这里注意一下 service若是没有起来 咱们是接收不到广播的 因此必定要保证接收的时候service是开启的,上例中的service是在打开activity时开启的 可是若是用户把手机关掉而后在开机 , 这样的话service就不是打开状态 这样就很是危险了由于这时scrvice就接收不到任何消息了除非用户再次进activity 才会帮他打开scrvice 因此咱们能够在用户开机后就直接将scrvice打开,具体的实现方式以下   

在AndroidManifest.xml中注册一个开机广播  这个广播系统只会在开机发出并且只会发出一次 因此咱们接收这个广播就能够知道手机是否为开机状态   

<receiver android:name=".MyBootReceiver" >    

      <intent-filter>    

         <action android:name="android.intent.action.BOOT_COMPLETED" />    

     </intent-filter>    

   </receiver>     

注意加入权限    

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />     

在BroadcastRecevier中接收开机广播  而后打开service 就能够实现开机启动service。   

public class MyBootReceiver extends BroadcastReceiver {    

   /**开机广播**/    

    static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";     

    @Override    

    public void onReceive(Context context, Intent intent) {    

    /**若是为开机广播则开启service**/    

    if (intent.getAction().equals(BOOT_COMPLETED)) {    

        Intent i = new Intent(context, MyService.class);    

        context.startService(i);    

    }     

    }    

}    

相关文章
相关标签/搜索