Android BroadcastReceiver常見監(jiān)聽整理
在Android開發(fā)應用過程中 Android BroadcastReceiver經(jīng)常會用到,所以抽時間整理了一番,省的后續(xù)在用到的時候再去百度。
BroadcastReceiver幾種常見監(jiān)聽
1.BroadcastReceiver監(jiān)聽撥號
<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,然后返回數(shù)據(jù)
setResultData("110"+call);
}
2.BroadcastReceiver監(jiān)聽短信
<receiver android:name="SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
3.BroadcastReceiver監(jiān)聽SD卡狀態(tài)
<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監(jiān)聽開機
<receiver android:name="BootCompeletedReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
5.BroadcastReceiver監(jiān)聽應用安裝卸載
<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實現(xiàn)網(wǎng)絡狀態(tài)實時監(jiān)聽
- Android BroadcastReceiver接收收到短信的廣播
- Android運用BroadcastReceiver實現(xiàn)強制下線
- Android BroadcastReceiver廣播注冊方式總結
- android之BroadcastReceiver應用詳解
- 深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動態(tài))詳解
- Android BroadcastReceiver廣播機制概述
- Android采取BroadcastReceiver方式自動獲取驗證碼
- 詳解Android中BroadCastReceiver組件
- Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡連接狀態(tài)的改變
相關文章
Android Selector 按下修改背景和文本顏色的實現(xiàn)代碼
這篇文章主要介紹了Android Selector 按下修改背景和文本顏色的實現(xiàn)代碼,本文通過實例代碼和demo展示給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Android中使用Handler及Countdowntimer實現(xiàn)包含倒計時的閃屏頁面
這篇文章主要介紹了Android中使用Handler及Countdowntimer實現(xiàn)包含倒計時的閃屏頁面,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
教你快速實現(xiàn)Android動態(tài)模糊效果
相信大家都發(fā)現(xiàn)了越來越多的App里面使用了模糊效果,比如雅虎天氣的界面,雖然我并不知道雅虎天氣是怎么做出這種效果的,但是簡單的模仿一下的話,還是能做到的。下面一起來學習學習。2016-08-08

