Android开发之发送有序广播+拦截广播
来源:互联网 发布:淘宝有库存管理吗 编辑:程序博客网 时间:2024/05/22 14:05
看这篇之前,先看一下我的上一遍博客,这些代码都是 第一行代码 里面的
新建一个BroadcastTest2项目
新建AndroidBroadcastReceiver 继承BroadcastReceiver
package com.example.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AnotherBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "receiver in AndoidBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
然后在AndroidManifest.xml对这个广播接收器进行注册
<receiver android:name=".AnotherBroadcastReceiver">
<intent-filter >
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
现在运行这个项目,在运行上一遍提到的项目,就会发现应用程序发出的广播会被其他应用程序接收到
接下来,开始,进行广播拦截
关了BroadcastTest2项目
修改上一篇中的BroadcastTest项目
package com.example.mybroadcastreceiver;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
//sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在AndroidManifest.xml修改优先级
<receiver android:name=".MyBroadcastReceiver">
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
最后修改MyBroadcastReceiver.java
package com.example.mybroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "received is MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
现在广播传到BroadcastTest 就拦截了,BroadcastTest2 就不在收到广播了。
- Android开发之发送有序广播+拦截广播
- Android 发送有序广播
- Android-发送有序广播
- Android 发送有序广播
- 发送自定义广播 之 有序广播
- 广播接收者之发送有序广播
- Android-广播接收者:发送有序广播
- Android--广播接收者(发送有序广播)
- Android 广播接收者练习-发送有序广播
- Android之有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- Android自定义发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- 【Android】安卓学习笔记之广播(三)发送标准广播和有序广播
- Android--有序广播的实例解析--案例《拦截有序广播》
- log4j日志配置
- android中点击事件的写法
- Java 征途:行者的地图
- 终于配置好Hadoop2.7.2的Eclipse开发环境了
- 用fieldset、legend实现文字写在边线上
- Android开发之发送有序广播+拦截广播
- leetcode287 Find the Duplicate Number
- Exynos4412 内核移植(二)—— 内核编译过程分析
- Linux中常用操作命令
- 分布式系统时钟同步技术的研究与应用
- Java 1.6 ThreadPoolExecutor源码解析
- 注入安卓进程,并hook java世界的方法
- java块
- 利用Spark Rdd生成Hfile直接导入到Hbase