Android中BroadcastReceiver使用总结

Android中BroadcastReceiver使用分为动态注册BroastcastReceiver ,静态注册BroastcastReceiver

1、动态注册BroastcastReceiver

定义BroadcastReceiver:

       private BroadcastReceiver bcr1 = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
       // TODO Auto-generated method stub
        String action = intent.getAction(); 
        Toast.makeText(context, "动态:"+action, 1000).show(); 
        
   
};

 注册registerReceiver(bcr1, new IntentFilter(ACTION_1));代码如下。关键代码是注册代码registerReceiver(bcr1, new IntentFilter(ACTION_1)),其他是一些简单的按钮, 及绑定的Click事件。

Button btn1,btn2,btn3; 
 static final String ACTION_1 = "com.example.broadcastreceiverdemo.ACTION_1"
 static final String ACTION_2 = "com.example.broadcastreceiverdemo.ACTION_2"
 static final String ACTION_3 = "com.example.broadcastreceiverdemo.ACTION_3"
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button)this.findViewById(R.id.button1);
    btn1.setOnClickListener(new ClickEvent());
     
    btn2 = (Button)this.findViewById(R.id.button2);
    btn2.setOnClickListener(new ClickEvent());
     
    btn3 = (Button)this.findViewById(R.id.button3);
    btn3.setOnClickListener(new ClickEvent());
     
    registerReceiver(bcr1, new IntentFilter(ACTION_1));
}

 发送广播

class ClickEvent implements OnClickListener {
 
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      if(v == btn1)
      {
          Intent intent = new Intent(ACTION_1);
          sendBroadcast(intent);
      }
      if(v == btn2)
      {
          Intent intent = new Intent(ACTION_2);
          sendBroadcast(intent);
      }
      if(v == btn3)
      {
          IntentFilter filter = new IntentFilter();
          filter.addAction(Intent.ACTION_BATTERY_LOW);
          filter.addAction(ACTION_3);
           
          registerReceiver(bcr2, new IntentFilter(ACTION_3));
           
          Intent intent = new Intent(ACTION_3);
          intent.putExtra("Conuntry", "China");
          intent.putExtra("City", "ShangHai");
          sendBroadcast(intent);
      }
  }

   其中 v == btn1为发送至内部动态注册的BroadcastReceiver;

       v == btn2为发送至内部静态注册的BroadcastReceiver;

   v == btn3为发送至系统级内部动态注册BroadcastReceiver(bt1和bt3基本一样,只是稍微加了addAction以及Intent的putExtra);

 

2 静态注册BroastcastReceiver。静态注册更要烦些,要在Androidmanifest.xml中注册。

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="cls2">
        <intent-filter>
            <action android:name="com.example.broadcastreceiverdemo.ACTION_2"></action>
        </intent-filter>
    </receiver>
    <activity
        android:name="com.example.broadcastreceiverdemo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
 
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

 并且要实现一个类继承BroadcastReceiver

public class cls2 extends BroadcastReceiver{
 
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction(); 
        Toast.makeText(context, "静态:"+action, 1000).show(); 
 
    }
}

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/14/2959367.html,如需转载请自行联系原作者

上一篇:LeetCode 5437. 不同整数的最少数目


下一篇:Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager