安卓实验课程——有序广播的实现。
来源:互联网 发布:什么java重写 编辑:程序博客网 时间:2024/05/22 13:04
实现有序广播:
首先做成界面的实现,如图:
插入一个BUtton按钮即可,代码实现如下:
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/stitch_one" tools:context="com.example.administrator.gz_hh.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:background="#FBFBFF" android:textSize="20dp" /></RelativeLayout>然后在MainActivity实现自定义广播发送,代码如下:
package com.example.administrator.gz_hh;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"); sendOrderedBroadcast(intent,null); }}添加广播接收者方便查看顺序广播的结果,代码如下:
public class MyBroadcastReceiverOneextends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverOne","自定义广播接收者one,接受到了广播事件"); }}按照 MyBroadcastReceiverOne 格式复制两个(将对应的名字修改)粘贴到对应的包中,如图:
然后在清单文件中添加三个receiver代码如下
<receiver android:name=".MyBroadcastReceiverOne"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter></receiver><receiver android:name=".MyBroadcastReceiverSecond"> <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>
做完这些便可运行查看结果了
输出顺序按照权限高低输出 顺序应该为一三二
结果如下图:
如果件将第二个的权限调高到与第一个相同时出现的结果如下图:
如果在MyBroadcastReceiverSecond(权限还是1000)中终结广播结果如下:可以看到第三个接受不到广播,如图:
阅读全文
1 0
- 安卓实验课程——有序广播的实现。
- 安卓 有序广播笔记
- 关于安卓发送有序广播的问题
- android有序广播的实现
- 安卓中的有序与无序广播
- 广播的类型————拦截有序广播
- 广播——BroadcastReceiver的有序广播和优先级
- 安卓BroadcastReceiver组件使用系列2:给多个广播接收者发送广播和有序广播的使用
- Android之广播机制—有序广播
- 案例——有序广播
- 有序广播案例实现
- 实现有序广播
- 有序广播案例实现
- 【Android】安卓学习笔记之广播(三)发送标准广播和有序广播
- 安卓通过有序广播拦截特定电话!
- 有序广播的使用方法
- Android菜鸟的成长笔记(26)——普通广播与有序广播
- 安卓开发:广播接收器的使用【实现传值】
- JS--写一个音乐播放页面(播放次数)
- 使用ES6的Set去除数组的重复元素
- NG机器学习之指数分布族
- 发送有序广播
- C语言通过ODBC读写Excel表格文件(ODBC)
- 安卓实验课程——有序广播的实现。
- Machine Learning_安装_caffe_再试一次
- 拷贝构造函数被调用的时机
- 使用单枚举类型实现单例
- gcc编译器 __attribute__ ((section(".")))
- Android studio 重复依赖去重
- Android顶部导航栏TabLayout
- java过滤器处理中文乱码问题
- java多线程中的sleep()方法与wait()方法的区别