赞
踩
(1)功能描述:实现拦截一条有序广播
(2)技术要点:通过sendOrderedMyBroadcast()方法发送一条有序广播
(3)实现步骤:设置优先级广播接收者的优先级
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/stitch_one"
tools:context="com.example.bz0209.m_application.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:onClick="send"
android:text="发送有序广播"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FBFBFF"
android:textSize="20sp"
/>
</RelativeLayout>

package com.example.bz0209.m_application;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent =new Intent();
//定义广播事件类型
intent.setAction("Intercept_Stitch");
//发送广播
sendBroadcast(intent,null);
}
}

(1)MyBroadcastReceiverOne.java
package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver {
public MyBroadcastReceiverOne() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("MyBroadcastReceiverOne","自定义广播接收者One,接收到了广播事件");
}
}
(2)MyBroadcastReceiverTwo.java
package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
public MyBroadcastReceiverTwo() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("MyBroadcastReceiverTwo","自定义广播接收者Two,接收到了广播事件");
}
}
(3)MyBroadcastReceiverThree.java
package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
public MyBroadcastReceiverThree() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("自定义广播接收者Three,接收到了广播事件", "MyBroadcastReceiverThree");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bz0209.m_application">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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=".MyBroadcastReceiverOne">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverTwo">
<intent-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverThree">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
</application>
</manifest>

(1)程序启动后,单击“发送有序广播”按钮,发出一条广播事件
(2)若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面,再来运行程序
(3)修改MyBroadcastReceiverTwo如下:
package com.example.bz0209.m_application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
public MyBroadcastReceiverTwo() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("MyBroadcastReceiverTwo","自定义广播接收者Two,接收到了广播事件");
abortBroadcast();
Log.i("MyBroadcastReceiverTwo","广播被我终结了");
}
}

结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。