您的位置:首页 > 其它

案例---拦截有序广播

2017-06-04 16:58 405 查看
一、功能描述:

实现拦截一条有序广播

二、技术要点:

通过sendOrderedBroadcast()方法发送一条有序广播

1、用户交互界面的设计与实现

2、界面交互代码的设计与实现

3、创建3个广播接受者

MyBroadcastReceiverOne.java

MyBroadcastReceiverTwo.java

MyBroadcastReceiverThree.java

4、设置优先级广播接受者的优先级

三、实现步骤:

1、设置广播接受者的优先级,在清单文件中设置,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.bz0209.orderedbroadcast">

    <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>

            <receiver android:name=".MyBroadcastReceiverOne">

            <intenf-filter android:priority="1000">

                <action android:name="Intercept_Stitch"/>

            </intenf-filter>

        </receiver>

            <receiver android:name=".MyBroadcastReceiverTwo">

                <intenf-filter android:priority="200">

                    <action android:name="Intercept_Stitch"/>

                </intenf-filter>

            </receiver>

            <receiver android:name=".MyBroadcastReceiverThree">

                <intenf-filter android:priority="600">

                    <action android:name="Intercept_Stitch"/>

                </intenf-filter>

            </receiver>

            <receiver

                android:name=".MyReceiver"

                android:enabled="true"

                android:exported="true">

                <intent-filter android:priority="100"/>

            </receiver>

        </activity>

    </application>

</manifest>

其中在清单文件中注册广播接收者<intent-filter android:priority="100"/>属性指定数值越大,优先级越高。如果两个广播接收者的优先级相同,则先注册的组件优先接收到广播。如果两个应用程序监听了同一个广播事件并设置了优先级,则先安装的应用优先接收到广播。

2、编写用户交互界面,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>

<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.orderedbroadcast.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>

3、界面交互代码的设计与实现

package com.example.bz0209.orderedbroadcast;

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_Switch");

        sendOrderedBroadcast(intent,null);

    }

}

4、注册三个广播接收者

<1>MyBroadcastReceiverOne.Java

package com.example.bz0209.orderedbroadcast;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class MyBroadcastReceiverOne extends BroadcastReceiver {

  @Override

    public void onReceive(Context context, Intent intent) {

        Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件");

    }

}

<2>MyBroadcastReceiverTwo.java

package com.example.bz0209.orderedbroadcast;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class MyBroadcastReceiverTwo extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

        Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");

abortBroadcast();//拦截有序广播

        Log.i("MyBroadcastReceiverTwo","我是接收者Two,广播被我终结了");

    }

}

<3>MyBroadcastReceiverThree.java

package com.example.bz0209.orderedbroadcast;

import android.annotation.SuppressLint;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class MyBroadcastReceiverThree extends BroadcastReceiver {

    @SuppressLint("LongLogTag")

    @Override

    public void onReceive(Context context, Intent intent) {

        Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");

    }

}

5、运行效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: