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{@Overridepublic 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic 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{@Overridepublic 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";@Overridepublic 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{@Overridepublic 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();}}}