SoFunction
Updated on 2025-04-10

Android BroadcastReceiver Common Listening and Organizing

During Android development and application process, Android BroadcastReceiver is often used, so I took time to sort it out and saved the subsequent use of it and then went to Baidu.

Several common listening types of BroadcastReceiver

Listen to dial

<intent-filter android:priority="1000" >
       <action android:name=".NEW_OUTGOING_CALL" />
</intent-filter>
 

@Override
  public void onReceive(Context context, Intent intent) {
    //Get the number of the call    String call=getResultData();
    //Prepare 110 before the phone number and return the data    setResultData("110"+call);
  } 

 Listen to text messages

<receiver android:name="SmsReceiver">
     <intent-filter android:priority="1000">
        <action android:name=".SMS_RECEIVED"></action>
     </intent-filter>
</receiver>
 

Monitor SD card status

<receiver Android:name=".SDStatusReceiver">
   <intent-filter >
     <action android:name=".MEDIA_MOUNTED"/>
     <action android:name=".MEDIA_REMOVED"/>
     <action android:name=".MEDIA_UNMOUNTED"/>
     <data android:scheme="file"/>
   </intent-filter>
</receiver


public class SDStatusReceiver extends BroadcastReceiver { 
  
  @Override 
  public void onReceive(Context context, Intent intent) { 
    //Judge what kind of broadcast is received    String action = (); 
    if(".MEDIA_MOUNTED".equals(action)){ 
      (context, "SD card available", 0).show(); 
    } 
    else if(".MEDIA_REMOVED".equals(action)){ 
      (context, "SD card unplugged", 0).show(); 
    } 
    else if(".MEDIA_UNMOUNTED".equals(action)){ 
      (context, "SD card is not available", 0).show(); 
    } 
  } 
} 

Monitoring boots

<receiver android:name="BootCompeletedReceiver">
      <intent-filter >
        <action android:name=".BOOT_COMPLETED"/>
      </intent-filter>
</receiver>
 

Listen to the application installation and uninstallation

<receiver android:name="IntallReceiver">
      <intent-filter >
        <action android:name=".PACKAGE_REMOVED"/>
        <action android:name=".PACKAGE_ADDED"/>
        <data android:scheme="package"></data>
      </intent-filter>
</receiver>
public class IntallReceiver extends BroadcastReceiver {&lt;br&gt;
  @Override
  public void onReceive(Context context, Intent intent) {
    String packageName = ().toString();
    String action = ();
    // If it's uninstall    if (".PACKAGE_REMOVED".equals(action)) {
        (context, packageName+"Application is uninstalled", 1).show();
        (packageName+"Deleted");
    } else if (".PACKAGE_ADDED".equals(action)) {
        (context, packageName+"Application Installation", 1).show();
        (packageName + "Installed");
    }
  }
}

Thank you for reading, I hope it can help you. Thank you for your support for this site!