Android BroadcastReceiver常见监听整理
短信预约 -IT技能 免费直播动态提醒
在Android开发应用过程中 Android BroadcastReceiver经常会用到,所以抽时间整理了一番,省的后续在用到的时候再去百度。
BroadcastReceiver几种常见监听
1.BroadcastReceiver监听拨号
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
@Override
public void onReceive(Context context, Intent intent) {
//获取拨打电话的号码
String call=getResultData();
//在电话号码前加上110,然后返回数据
setResultData("110"+call);
}
2.BroadcastReceiver监听短信
<receiver android:name="SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
3.BroadcastReceiver监听SD卡状态
<receiver Android:name=".SDStatusReceiver">
<intent-filter >
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver
public class SDStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//判断收到的到底是什么广播
String action = intent.getAction();
if("android.intent.action.MEDIA_MOUNTED".equals(action)){
Toast.makeText(context, "SD卡可用", 0).show();
}
else if("android.intent.action.MEDIA_REMOVED".equals(action)){
Toast.makeText(context, "SD卡拔出", 0).show();
}
else if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
Toast.makeText(context, "SD卡不可用", 0).show();
}
}
}
4.BroadcastReceiver监听开机
<receiver android:name="BootCompeletedReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
5.BroadcastReceiver监听应用安装卸载
<receiver android:name="IntallReceiver">
<intent-filter >
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
public class IntallReceiver extends BroadcastReceiver {<br>
@Override
public void onReceive(Context context, Intent intent) {
String packageName = intent.getData().toString();
String action = intent.getAction();
// 如果是卸载
if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
Toast.makeText(context, packageName+"应用程序被卸载", 1).show();
System.out.println(packageName+"已删除");
} else if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
Toast.makeText(context, packageName+"应用程序安装", 1).show();
System.out.println(packageName + "已安装");
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:Android BroadcastReceiver实现网络状态实时监听Android BroadcastReceiver接收收到短信的广播Android运用BroadcastReceiver实现强制下线Android BroadcastReceiver广播注册方式总结android之BroadcastReceiver应用详解深入Android中BroadcastReceiver的两种注册方式(静态和动态)详解Android BroadcastReceiver广播机制概述Android采取BroadcastReceiver方式自动获取验证码详解Android中BroadCastReceiver组件Android使用BroadcastReceiver监听网络连接状态的改变
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341