您的位置:首页 > 移动开发 > Android开发

android通信机制3.广播的发送与接收

2016-11-17 14:37 513 查看
3.广播的发送与接收

Android开发中如果须要对两个完全没关系的程序之间进行通信 就可以使用发送广播与接收广播的机制来实现 ,例如程序A发送了一个广播 程序B接受到 做一些事情 这样就达到了相互的通讯。
public class BroadcastActivity extends Activity {  
  
  
     
    Button mButton0 = null;  
    Button mButton1 = null;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
    setContentView(R.layout.broadcast);  
      
    mButton0 = (Button)findViewById(R.id.button0);  
    mButton0.setOnClickListener(new OnClickListener() {  
          
        @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中可以过滤只接收须要接收的广播、

view plain
<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>  

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

view plain
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中注册一个开机广播  这个广播系统只会在开机发出而且只会发出一次 所以我们接收这个广播就可以知道手机是否为开机状态

view plain
<receiver android:name=".MyBootReceiver" >  
      <intent-filter>  
         <action android:name="android.intent.action.BOOT_COMPLETED" />  
     </intent-filter>  
   </receiver>  

注意加入权限

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

在BroadcastRecevier中接收开机广播  然后打开service 就可以实现开机启动service。

view plain
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);  
    }  
  
    }  
}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息