android 广播机制

1:首先说andoid 广播分为系统的和 自定义的

2:注册方式呢,也是两种,1:静态注册,在manifest.xml 文件中注册的  2:动态注册,用filter 区分

不说了 占代码

 

首先是动态注册:

 

BroadReceiver re = new BroadReceiver();  // 自己定义的接收器
    
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.myselfAction");
    NotifyActivity.this.registerReceiver(re, intentFilter);
    
    Intent mIntent = new Intent();
   
    mIntent.setAction("com.myselfAction");
    sendBroadcast(mIntent); 

 

 

2:静态注册:

Intent mIntent = new Intent(NotifyActivity.this,BroadReceiver.class);
    
    mIntent.setAction("com.myselfAction");

 

 

public class BroadReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equals("com.myselfAction")){
   Toast.makeText(context, "deddee", Toast.LENGTH_LONG).show();
  }
 }
 

 

 

 

  <receiver android:name="com.example.slide.receiver."></receiver>

 

你可能感兴趣的:(android)