1.看這篇文章之前請(qǐng)先看上一篇
http://www.itdecent.cn/p/fbe6ee0af9c3?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=qq
好,我們回歸正題
首先,我們建立一個(gè)項(xiàng)目,用來發(fā)送,建立一個(gè)按鈕用來充當(dāng)觸發(fā)器
添加MainActivity中代碼,給按鈕添加監(jiān)聽然后發(fā)送代碼如下
package mylistview.lmq.cn.broadcasttest2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity01 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main01);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
}
});
}
}
2.我們新建一個(gè)項(xiàng)目,按照第一個(gè)文章的方法建立一個(gè)接收者
在其中顯示信息,我這里打印為 i have already receiver。
代碼同第一篇文章,我將其貼在這,注意發(fā)送和接受的廣播是同一種,這里為com.example.broadcasttest.MY_BROADCAST
package mylistview.lmq.cn.broadcastreciver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context,"i hava aleadly reciver",Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mylistview.lmq.cn.broadcastreciver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
3.分別運(yùn)行兩個(gè)程序后,點(diǎn)擊發(fā)送者的按鈕,你會(huì)發(fā)現(xiàn)底下會(huì)打印出一條信息
4.發(fā)送有序廣播
在你建立多個(gè)接收者的時(shí)候你會(huì)發(fā)現(xiàn),打印信息都會(huì)出來,但是順序是怎樣的呢?所以這里我們給他們規(guī)定一下優(yōu)先級(jí)
在接收者的配置文件中加上優(yōu)先級(jí)代碼priority=""如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mylistview.lmq.cn.broadcastreciver2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity01">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">//這里我添加的是100
//,數(shù)字越大,優(yōu)先級(jí)越高
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
運(yùn)行一下你發(fā)現(xiàn)添加有優(yōu)先級(jí)的會(huì)在前面顯示了
5.我們可不可以中斷呢?我只想顯示一條或幾條,因?yàn)槭前凑諆?yōu)先級(jí)的順序進(jìn)行顯示,所以你哪一個(gè)想要阻斷,后面優(yōu)先級(jí)低的就不會(huì)顯示了,
很簡單添加一個(gè)方法就可以
abortBroadcast();
package mylistview.lmq.cn.broadcastreciver2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context,"i hava alrealy reciver 2",Toast.LENGTH_SHORT).show();
abortBroadcast();//阻斷傳遞
}
}