1.看这篇文章之前请先看上一篇
http://www.jianshu.com/p/fbe6ee0af9c3?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=qq
好,我们回归正题
首先,我们建立一个项目,用来发送,建立一个按钮用来充当触发器
添加MainActivity中代码,给按钮添加监听然后发送代码如下
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.我们新建一个项目,按照第一个文章的方法建立一个接收者
在其中显示信息,我这里打印为 i have already receiver。
代码同第一篇文章,我将其贴在这,注意发送和接受的广播是同一种,这里为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.分别运行两个程序后,点击发送者的按钮,你会发现底下会打印出一条信息
4.发送有序广播
在你建立多个接收者的时候你会发现,打印信息都会出来,但是顺序是怎样的呢?所以这里我们给他们规定一下优先级
在接收者的配置文件中加上优先级代码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
//,数字越大,优先级越高
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
运行一下你发现添加有优先级的会在前面显示了
5.我们可不可以中断呢?我只想显示一条或几条,因为是按照优先级的顺序进行显示,所以你哪一个想要阻断,后面优先级低的就不会显示了,
很简单添加一个方法就可以
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();//阻断传递
}
}
网友评论