Android Broadcastreceiver Common listening and finishing _android

Source: Internet
Author: User

Android Broadcastreceiver is often used in Android development applications, so take time to tidy up, the province's follow-up in the use of the time to go to Baidu.

Broadcastreceiver several common monitoring

1.BroadcastReceiver Listening dialing

<intent-filter android:priority= "1000" >
       <action android:name= "Android.intent.action.NEW_OUTGOING_" Call "/>
</intent-filter>
 

@Override public
  void onreceive (context, Intent Intent) {
    //Get dialing number
    String call=getresultdata () ;
    Add 110 to the phone number before returning the data
    setresultdata ("+call");
  

2.BroadcastReceiver Listening SMS

<receiver android:name= "Smsreceiver" >
     <intent-filter android:priority= "1000" >
        <action Android:name= "Android.provider.Telephony.SMS_RECEIVED" ></action>
     </intent-filter>
</ Receiver>
 

3.BroadcastReceiver Monitor SD card status

<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

The public class Sdstatusreceiver extends Broadcastreceiver { 
  
  @Override 
  the public void onreceive Intent Intent) { 
    //Judge what is received in the end broadcast 
    String action = intent.getaction (); 
    if ("Android.intent.action.MEDIA_MOUNTED". Equals (Action)) { 
      Toast.maketext (context, "SD card is available", 0). Show () 
    ; 
    else if ("Android.intent.action.MEDIA_REMOVED". Equals (Action)) { 
      Toast.maketext (context, "SD card Unplugged", 0). Show (); 
    else if ("Android.intent.action.MEDIA_UNMOUNTED". Equals (Action)) { 
      Toast.maketext (context, "SD card not available", 0). Show ();}} 
 
  

4.BroadcastReceiver Monitor Boot

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

5.BroadcastReceiver Monitoring application Ann loading and unloading

<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 it is uninstall if
    ("Android.intent.action.PACKAGE_REMOVED". Equals (Action)) {
        Toast.maketext (context, PackageName + "Application uninstalled", 1). Show ();
        System.out.println (packagename+ "deleted");
    } else if ("Android.intent.action.PACKAGE_ADDED". Equals (Action)) {
        Toast.maketext (context, packagename+ "Application Installation" , 1). Show ();
        System.out.println (PackageName + "Installed");}
    }

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.