Broadcast发送有序广播

这里一般和有序的代码全部罗列出来 但本章侧重于关注有序广播的发送与接收内容

发送端main-xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendNormalBC"
        android:text="发送一般广播" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendOrderBC"
        android:text="发送有序广播" />
</LinearLayout>

发送端Activity代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendNormalBC(View v) {
        Log.i("godv", "发送一般广播");
        //隐式意图 跟接收器的action对应
        Intent intent = new Intent("godv");
        intent.putExtra("MESSAGE", "godv");
        intent.setPackage("com.example.broadcastr");
//        intent.setClassName("com.example.broadcastr",);
//        "com.example.broadcastr.MyR1eceive"
        sendBroadcast(intent);
        Toast.makeText(this, "发送一般广播", Toast.LENGTH_SHORT).show();
    }

    public void sendOrderBC(View v) {
        Log.i("godv", "发送有序广播");
        //隐式意图 跟接收器的action对应
        Intent intent = new Intent("godv2");
        intent.putExtra("MESSAGE", "godv");
        intent.setPackage("com.example.broadcastr");
//        intent.setClassName("com.example.broadcastr",);
//        "com.example.broadcastr.MyR1eceive"
        //权限先设置为null
        sendOrderedBroadcast(intent, null);
        Toast.makeText(this, "发送有序广播", Toast.LENGTH_SHORT).show();
    }

}

接收端新建两个类继承BroadcastReceiver因为是同样的代码所以这里只展示一个

public class MyR3eceive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("MESSAGE");
        Log.i("godv", "MyR3eceive--接收广播" + message);
    }

    public MyR3eceive() {
        Log.i("godv", "MyR3eceive--创建");
    }
}

注册这两个接收端(静态)

<receiver
    android:name=".MyR3eceive"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="1000">
        <action android:name="godv2" />
    </intent-filter>
</receiver>
<receiver
    android:name=".MyR4eceive"
    android:enabled="true"
    android:exported="true">
    <!--优先级android:priority int类型-->
    <intent-filter android:priority="2147483647">
        <action android:name="godv2" />
    </intent-filter>
</receiver>
</application>

最后放一个函数用来中断有序广播

public class MyR4eceive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("MESSAGE");
        Log.i("godv", "MyR4eceive--接收广播" + message);
        //如果是有序广播
        if (isOrderedBroadcast()) {
            //中断
            abortBroadcast();
        }
    }

    public MyR4eceive() {
        Log.i("godv", "MyR4eceive--创建");
    }
}

猜你喜欢

转载自blog.csdn.net/we1less/article/details/107743031
今日推荐