Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android BroadcastReceiver常見監聽整理

Android BroadcastReceiver常見監聽整理

編輯:關於Android編程

         在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 + "已安裝");
    }
  }
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved