2、註冊相關
一、靜態註冊實例程序
你們可能會問,什麼叫靜態註冊實例程序,先不要管上面的標題,慢慢往下看,後面在講動態註冊時會再提到。html
先構造一個接收器:java
[java] view plain copyandroid
- public class MyReceiver extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
-
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- String msg = intent.getStringExtra("msg");
- Log.i(TAG, "MyReceiver:"+msg);
-
- }
-
- }
直 接派生自BroadcastReceiver,在OnReceive()函數中進行處理便可,咱們前面說了,廣播的傳遞是靠Intent 的,OnReceive的第二個參數,就是廣播傳過來的Intent,由於後面咱們在發送廣播時,會利用PutStringExtra放進去一個標識爲 msg的字符串,因此這裏咱們能夠利用GetStringExtra把這個字符串取出來。而後用Log標記下這個類接收到了這個消息,以便咱們跟蹤。
你們可能會想,就這麼着,就能收到廣播了?固然不是,上面咱們說了,經過隱式Intent來發送廣播的,咱們確定要匹配這個Intent啊,匹配Intent的術語是Activity中的,在廣播這裏,叫要註冊,也就是要註冊一下,什麼樣的Intent能接收。app
MyReceiver的廣播接收 註冊代碼以下:(靜態註冊)less
- <receiver android:name=".MyReceiver">
- <intent-filter>
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
android:name:對應接收器的類名;咱們自定義的類名叫MyReceiver ,因此這裏寫".MyReceiver "async
intent-filter標籤裏,一樣是必須的兩項:action和category;我在這裏自定義了action的名字,等下隱式發送經過時,就是利用匹配action來接收通知的。ide
此時的AndroidManifest.xml所有內容爲:函數
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.test_brocast_blog"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="14" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".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=".MyReceiver">
- <intent-filter>
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
- </application>
-
- </manifest>
這裏特別注意一個<activity> 標籤與<receiver>標籤的構造。徹底相同!!!!!!!
徹底相同體如今:佈局
- 一、所處位置:都直屬<application>標籤;
- 二、參數構造基本同樣;都有android:name,都有<intent-filter>;
- 三、都是經過Intent傳遞參數,也都是經過Intent進行匹配!!!!
這說明了一個問題:receiver是activity的變種!!!!!我沒有研究源碼,但僅從這些相同點來看,他們確定是從一個共同的類派生出來的。(猜測)測試
最後是發送廣播:
咱們在主頁面加一個Button,當點擊Button時發送廣播消息。
佈局文件以下:(activity_main.xml)
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.test_brocast_blog.MainActivity" >
-
- <Button
- android:id="@+id/sent_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="發送Broadcast" />
-
- </RelativeLayout>
代碼以下:(MainActivity.java)
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btn= (Button)findViewById(R.id.sent_btn);
- btn.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- send();
- }
- });
-
- }
-
- public void send() {
- Intent intent = new Intent("android.intent.action.MY_BROADCAST");
- intent.putExtra("msg", "hello receiver.");
- sendBroadcast(intent);
- }
- }
真 正的發送代碼在send()函數中,在這個函數中能夠看到,咱們經過傳進去剛纔註冊的MyRecevier的action,來構造一個隱式Intent, 而後利用PutExtra放進去一個額外信息(這個不是必須的,咱們僅僅是爲了跟蹤這個消息傳到了哪裏去,在《詳解Intnent》系統文章中有講怎樣構 造一個隱式Intent),與StartActivity不一樣的是,這裏利用的是sendBroadcast(intent)來發送這個Intent;
效果圖:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
這裏注意一下應用名!!!!(com.example.test_brocast_blog)後面會用到。
二、動態註冊實例程序
前面,咱們說了靜態註冊,什麼叫靜態註冊呢,就是利用XML來註冊。
相反,利用代碼來註冊的就叫動態註冊。
靜態註冊和動態註冊是有區別的,主要體如今接收上。
靜態註冊的程序,不管該程序是否啓動,都會當廣播到來時接收,並處理。而動態註冊的程序只有在程序運行時纔會收到廣播消息,程序不運行了,它就收不到了。
動態註冊的代碼以下:
- MyReceiver receiver = new MyReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction("android.intent.action.MY_BROADCAST");
-
- registerReceiver(receiver, filter);
同 樣,首先生成咱們要接收的類的實例,而後利用IntentFilter來聲明他能夠匹配的廣播類型(這裏利用動做來匹配),最後利用 registerReceiver(receiver, filter);來註冊,即利用當哪一種類型的Intent廣播到來時,要調用MyReceiver類。
注意:發送廣播以前,要先註冊,否則根本沒有接收者匹配,固然,不註冊接收者也不會出現任何錯誤或警告,只是發送一個沒有任何接收者的廣播播毫無心義。
下面咱們新建一個項目,取名叫:Test_Brocast_Blog_Dynamic
一樣,寫一個MyRecever類來接收廣播,MyRecever類內容不變:
- public class MyReceiver extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
-
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- String msg = intent.getStringExtra("msg");
- Log.i(TAG, msg);
- }
-
- }
而後一樣,給MainActivity佈局裏添加一個Button,當點擊Button時發送廣播。佈局文件與上面同樣,這裏我只寫代碼:
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- //在發送以前,肯定在代碼的某個位置已經動態註冊
- MyReceiver receiver = new MyReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction("android.intent.action.MY_BROADCAST");
-
- registerReceiver(receiver, filter);
-
-
- //發送廣播
- Button btn= (Button)findViewById(R.id.sent_btn);
- btn.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- send();
- }
- });
-
-
-
- }
-
- public void send() {
- Intent intent = new Intent("android.intent.action.MY_BROADCAST");
- intent.putExtra("msg", "hello receiver.");
- sendBroadcast(intent);
- }
-
- }
在OnCreate()裏在註冊廣播接收者,即告訴系統,當這個廣播到來時,用MyReciver來接收。而後點擊發送按鈕來發送廣播。
注:(在運行這個程序以前,先把靜態註冊的APP裝到手機上,這是下面得出結論的前提)
結果以下:
操做界面
![](http://static.javashuo.com/static/loading.gif)
結果:
![](http://static.javashuo.com/static/loading.gif)
咦?怎麼出來兩條信息?
也就是說有兩個接收者接收到了這條廣播,但咱們這裏只註冊了一個MyRecever實例啊。
對的,看應用名稱就能夠看得出,這是兩個不一樣的應用,有一個是靜態註冊的(com.example.test_brocast_blog),因此靜態註冊的程序不論是否啓動,均可以收穫得匹配的廣播的,並對這個廣播操做。
若是想試一下,動態註冊的代碼能不能收到廣播,能夠反過來一下,運行靜態註冊的程序,把動態註冊的程序關掉,看出來幾條Log?答案確定是一條!由於咱們都知道動態註冊的代碼在程序不運行時是收不到廣播的。
3、普通廣播與有序廣播
普通廣播是指你們等級都是同樣的,當廣播到來時,都能一塊接收到,並無接收的前後順序。因爲是一同接收到的,因此一個接收者是沒有辦法阻止另外一個接收者接收這個廣播的。
有序廣播是指接收是按必定的優先級順序來接收的,優先級高的先收到,並能夠對廣播進行操做後,再傳給下一個接收者,固然也能夠不傳,若是不傳的話,後面的接收者就都收不到這個廣播了。
普通廣播
(在運行這個程序以前,先把手機上前兩個APP所有刪除,以避免影響結果)
首先咱們建三個接收者,並對他們所有靜態註冊。三個接收者的代碼分別以下:
FirstRecever:
- public class FirstRecever extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- String msg = intent.getStringExtra("msg");
- Log.i(TAG, "FirstRecever:"+msg);
- }
- }
SecondRecever:
- public class SecondRecever extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- String msg = intent.getStringExtra("msg");
- Log.i(TAG, "SecondRecever:"+msg);
- }
- }
MyReceiver
- public class MyReceiver extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
-
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- String msg = intent.getStringExtra("msg");
- Log.i(TAG, "MyReceiver:"+msg);
- }
- }
註冊代碼以下:(AndroidManifest.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.test_brodcast"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="14" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".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=".MyReceiver">
- <intent-filter>
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- <receiver android:name=".FirstRecever">
- <intent-filter>
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- <receiver android:name=".SecondRecever">
- <intent-filter>
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- </application>
-
- </manifest>
一樣,咱們在MainActivity中添加一個Button,當點擊按鈕時發送廣播,MainActivity代碼以下:
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btn= (Button)findViewById(R.id.sent_btn);
- btn.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- send();
- }
- });
-
- }
-
- public void send() {
- Intent intent = new Intent("android.intent.action.MY_BROADCAST");
- intent.putExtra("msg", "hello receiver.");
- sendBroadcast(intent);
- }
-
- }
運行程序,結果:
![](http://static.javashuo.com/static/loading.gif)
可見,三個全都收到了廣播。
有序廣播(無訪問權限版)
首先有序廣播與普通廣播的不一樣點在發送和接收都有不一樣。
首在發送有序廣播:
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btn= (Button)findViewById(R.id.sent_btn);
- btn.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- send();
- }
- });
-
- }
-
- public void send() {
-
- Intent intent = new Intent("android.intent.action.MY_BROADCAST");
- intent.putExtra("msg", "hello receiver.");
- sendOrderedBroadcast(intent, null); //沒有添加權限
- }
- }
在前面的各個例子中,咱們發送廣播都是用的:sendBroadcast(intent); 而這裏卻用的是:sendOrderedBroadcast(intent, null);
對這個函數的官方解釋是:
public abstract void sendOrderedBroadcast (Intent intent, String receiverPermission)
Added in API level 1
Broadcast the given intent to all interested BroadcastReceivers, delivering them one at a time to allow more preferred receivers to consume the broadcast before it is delivered to less preferred receivers. This call is asynchronous; it returns immediately, and you will continue executing while the receivers are run.
See BroadcastReceiver
for more information on Intent broadcasts.
Parameters
intent |
The Intent to broadcast; all receivers matching this Intent will receive the broadcast. |
receiverPermission |
(optional) String naming a permissions that a receiver must hold in order to receive your broadcast. If null, no permission is required. |
其中第二個參數是指定接收者必須擁有的接收權限,若是設爲NUll,就是不須要接收權限,全部匹配的Receiver都能接收到。咱們這裏先不須要權限試試看,下面再舉個須要權限的例子。
接收端
咱們上面說了,接收端必須是有序的,是有優先級的,這種優先級是在註冊時配置的,好比:
- <receiver android:name=".FirstRecever">
- <intent-filter android:priority="10">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- lt;/receiver>
與上面靜態註冊的不一樣在於,在Intent-filter中添加一個android:priority="10"屬性,這個就是接收器優先級,數字越大的接收器,優先級越高,越先接到廣播。
一樣,上面咱們三個類FirstRecever、SecondRecever和MyReceiver 的註冊文件代碼以下 :
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.testbroast_order"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="14" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".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=".FirstRecever">
- <intent-filter android:priority="10">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- <receiver android:name=".SecondRecever">
- <intent-filter android:priority="9">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- <receiver android:name=".MyReceiver">
- <intent-filter android:priority="8">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
- <!-- 接收優先級逐級下降 -->
- </application>
-
- </manifest>
最後是代碼部分
前面我也曾提到,在一個接收器收到發來的Intent後,能夠對其進行更改,對發送來的廣播Intent進行修改是利用setResultExtras(bundle); 函數來實現的。
public final void setResultExtras (Bundle extras)
Added in API level 1
Change the current result extras of this broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast
. This is a Bundle holding arbitrary data, whose interpretation is up to the broadcaster. Can be set to null. Calling this method completely replaces the current map (if any).
This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast
Parameters
extras |
The new extra data map; may be null. |
翻譯一下:
這個函數是用來改變當前廣播傳來的Extra額外信息的;它只能經過Context.sendOrderedBroadcast
.發送過來的廣播有效;它使用Bundle來傳遞任意的數據,而這些數據只有接收器(broadcaster)才能解析。固然也能夠把它設置爲NULL,這樣,它就把傳來的數據映射所有清空了。
參數:
extras:新的數據映射,能夠爲空。
從上面的優先級能夠看出,這裏三個類的接收順序是這樣的:FirstRecever-》SecondRecever-》MyReceiver
下面看看FirstRecever如何利用setResultExtras來改變傳來的Msg信息:
- public class FirstRecever extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- //先得到傳過來的MSG
- String msg = intent.getStringExtra("msg");
- Log.i(TAG, "FirstRecever:"+msg);
-
- //更改廣播數據
- Bundle bundle = new Bundle();
- bundle.putString("msg", msg + "@FirstReceiver");
- setResultExtras(bundle);
- }
- }
首先,咱們利用getStringExtra()得到傳過來的msg消息,而後利用bundle從新封裝一個以「msg」爲key的消息,把"@FirstReceiver"添加到消息裏,表示通過了這裏。
最後利用setResultExtras(bundle); 修改當前的結果集。
若是想終止消息往下一個接收器傳遞,可使用:abortBroadcast(); //終止消息再傳遞
這裏有一個疑問:我利用setResultExtras(bundle); 修改傳送結果,對原來廣播過來的數據有影響嗎?下面咱們就在SecondRecever中作個測試
下面看看另外兩個接收器代碼:SecondRecever
- public class SecondRecever extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- //先得到廣播過來的MSG
- String broadcast_msg = intent.getStringExtra("msg");
- Log.i(TAG, "SecondRecever--broadcast_msg:"+broadcast_msg);
-
- //接收經過setResultExtras傳過來的msg
- String msg = getResultExtras(true).getString("msg");
- Log.i(TAG, "SecondReceiver: " + msg);
-
- //修改setResultExtras傳來的結果
- Bundle bundle = new Bundle();
- bundle.putString("msg", msg + "@SecondReceiver");
- setResultExtras(bundle);
- }
- }
這裏先經過intent.getStringExtra("msg"); 得到廣播過來的數據;
而後再利用getResultExtras(true).getString("msg"); 得到上一級傳過來的setResultExtras(bundle); 裏的數據;
最後從新將bundle裏的數據中添加"@SecondReceiver"作個標記;
最後,MyReceiver:
- public class MyReceiver extends BroadcastReceiver {
-
- private static final String TAG = "MyReceiver";
-
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
-
- String msg = getResultExtras(true).getString("msg");
- Log.i(TAG, "MyReceiver: " + msg);
- }
- }
到這就全部就序了,運行下代碼:
![](http://static.javashuo.com/static/loading.gif)
從結果能夠看出:經過setResultExtras(bundle); 傳遞的數據是不會更改原生廣播的數據的。也只是原來廣播數據中額外添加的數據。
有序廣播(添加訪問權限版)
前面咱們看到在sendOrderedBroadcast(intent, null); 中,第二個參數能夠設定訪問權限,在上個例子中,咱們並無加入訪問權限,下面咱們就發送一個帶權限的廣播:
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btn= (Button)findViewById(R.id.sent_btn);
- btn.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- send();
- }
- });
-
- }
-
- public void send() {
-
- Intent intent = new Intent("android.intent.action.MY_BROADCAST");
- intent.putExtra("msg", "hello receiver.");
- sendOrderedBroadcast(intent, "harvic.broadcast.perssion");
- }
- }
這段代碼中,咱們利用 sendOrderedBroadcast(intent, "harvic.broadcast.perssion"); 發送一個必須擁有"harvic.broadcast.perssion"權限的接收器才能接收到咱們的廣播;
而後咱們要在接收器中加入聲明使用權限的代碼:
有關權限的聲明與使用,能夠參考這篇文章:《聲明、使用與自定義權限》
首先建立一個"harvic.broadcast.perssion"權限
- <permission android:name="harvic.broadcast.perssion" android:protectionLevel="normal"></permission>
而後是底部聲明,咱們要使用這個權限:
- <uses-permission android:name="harvic.broadcast.perssion"/>
因此整體的代碼以下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.testbroadcast_order_perssion"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="14" />
- <permission android:name="harvic.broadcast.perssion" android:protectionLevel="normal"></permission>
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".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=".FirstRecever" >
- <intent-filter android:priority="10">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- <receiver android:name=".SecondRecever" >
- <intent-filter android:priority="9">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
-
- <receiver android:name=".MyReceiver" >
- <intent-filter android:priority="8">
- <action android:name="android.intent.action.MY_BROADCAST"/>
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </receiver>
- <!-- 接收優先級逐級下降 -->
-
- </application>
-
- <!-- 若是不添加使用權限聲明,那麼接收器會拒絕接受消息的,因此在Log中不會有任何顯示 -->
- <uses-permission android:name="harvic.broadcast.perssion"/>
-
- </manifest>
其它接收器代碼不變,運行以後:
![](http://static.javashuo.com/static/loading.gif)
有 個地方要注意:即使像咱們如今這樣,本身的應用發出廣播給本身接收,但若是不聲明使用權限,是不能接收到廣播的。這點與Activity的權限機制不一 樣,在Activity中,只要在同一個應用中相互App跳轉,是不須要聲明使用權限的,權限的限制只針對其它應用調用此Activity。
好啦,這篇文章到這就結束了。本篇內容比較多,涉及到的代碼工程也比較多,現將相關源碼列表以下:
一、靜態註冊源碼
二、動態註冊源碼
三、普通接收源碼
四、有序廣播(無訪問權限)源碼
五、有序廣播(添加訪問權限)源碼
六、本文所用圖片
注意:在OnReceive中保存傳過來值的問題:
若是有下面一段僞代碼:
class xxxx{
private int mData=1;
public void onReceive(Context context, Intent intent)
Log.d("tag",mData + "");
mData = intent.getIntExtra("intdata",-1);
}
若是咱們經過intent傳過來的值保存在mData中,在下次再來的時候先打出來mData的值,會發現,每次打出來的都是1,因此根本沒有辦法經過成員變量保存onReceive中傳過來的值。