BroadcastReceiver是Android系统的四大组件之一,BroadcastReceiver是一个全局的系统级监听器,它拥有自己的独立进程。
我们来写一个最简单的广播接收过程
先在manifest中定义一个广播接受者
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.broadcasttest.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> <receiver android:name="com.example.broadcasttest.MyBroadCast"> <intent-filter > <action android:name="com.meritit.action.MY_BROADCAST"/> </intent-filter> </receiver> </application>广播接收者
public class MyBroadCast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的值为:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show(); } }向广播接收者发送广播
package com.example.broadcasttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(); intent.setAction("com.meritit.action.MY_BROADCAST"); intent.putExtra("msg", "阳光小强"); sendBroadcast(intent); } }); } }运行结果:
上面的例子是一个普通的广播接受者,下面我们来修改一下代码看看有序的广播接受者
<receiver android:name="com.example.broadcasttest.MyBroadCast"> <intent-filter android:priority="20"> <action android:name="com.meritit.action.MY_BROADCAST"/> </intent-filter> </receiver> <receiver android:name="com.example.broadcasttest.MyBroadCast2"> <intent-filter android:priority="0" > <action android:name="com.meritit.action.MY_BROADCAST"/> </intent-filter> </receiver>两个广播接收者设置了优先级,上面的优先级比下面的高
Intent intent = new Intent(); intent.setAction("com.meritit.action.MY_BROADCAST"); intent.putExtra("msg", "阳光小强"); sendOrderedBroadcast(intent, null);发送有序广播,注意是sendOrderedBroadcast
public class MyBroadCast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的值为:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show(); Bundle bundle = new Bundle(); bundle.putString("msg2", "有序的广播"); setResultExtras(bundle); //如果不想继续传播 //abortBroadcast(); } }优先级高的MyBroadCast先接收到,有序广播接收者可以添加新数据给下个等级的接受者。这种形式就有点像拦截器。
public class MyBroadCast2 extends BroadcastReceiver{ private static final String TAG = "broadcast"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = getResultExtras(true); String msg = intent.getStringExtra("msg"); String msg2 = bundle.getString("msg2"); Log.i(TAG, msg); Log.i(TAG, msg2); } }
最后输出结果:
除了接收用户发送的广播之外,BroadcastReceiver还有一个重要作用,就是接收系统广播。
详细请看:http://developer.android.com/reference/android/content/Intent.html
下面列出Android常见的广播。
ACTION_TIME_TICK
ACTION_TIME_CHANGED 系统时间被改变
ACTION_TIMEZONE_CHANGED 系统时区被改变
ACTION_BOOT_COMPLETED 系统启动完成
ACTION_PACKAGE_ADDED 系统添加包
ACTION_PACKAGE_CHANGED 系统的包改变
ACTION_PACKAGE_REMOVED 系统的包被删除
ACTION_PACKAGE_RESTARTED 系统的包被重启
ACTION_PACKAGE_DATA_CLEARED 系统的包数据被清空
ACTION_UID_REMOVED
ACTION_BATTERY_CHANGED 电池电量改变
ACTION_POWER_CONNECTED 系统连接电池
ACTION_POWER_DISCONNECTED 系统与电源断开
ACTION_SHUTDOWN 系统被关闭
<receiver android:name="com.example.broadcasttest.MyBroadCast3"> <intent-filter > <action android:name="android.intent.action.ACTION_BATTERY_LOW"/> </intent-filter> </receiver>
public class MyBroadCast3 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); int current = bundle.getInt("level"); int total = bundle.getInt("scale"); if(current * 1.0 / total < 0.15){ Toast.makeText(context, "电池电量过低,请尽快充电", Toast.LENGTH_LONG).show(); } } }