One, broadcast receiver broadcast receiver
Note: The four components of Android:
*activity Interface for user interaction
*content provider exposes application-private data to other applications
* Broadcast receiver Broadcast receiver
* Service back-office services
>android the radio recipients inside the phone
> System power shortage, battery full, plug in the charger, SD card is unplugged, SD card plugged in, dial out the phone, received a text message, boot complete, screen lock, screen unlock
> There are a lot of system events in the Android operating system, Google engineers want to tell the programmer (Android system built-in radio station), the programmer registers the radio to get the corresponding event
Example 1: Listen for outgoing calls from the user, get outbound phone broadcast events (manifest file configuration)
-
Buy a radio
Write a class inheritance Broadcastreceiver
Outcallreceiver extends Broadcastreceiver
2. Buy a battery
<receiver android:name= "Com.xunfang.ipdail.OutCallReceiver" >
</receiver>
3. Adjust to the appropriate channel
<intent-filter >
<action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
Example 2: SMS Detection Broadcast
Get all the messages your users receive
Object[] Objs = (object[]) Intent.getextras (). Get ("PDUs");
Loop to get text messages
for (Object Obj:objs) {
Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) obj);
Get the content of the text message
String BODY = Sms.getmessagebody ();
Get the address of the message
String address = sms.getoriginatingaddress ();
Get the message sent time
Long date = Sms.gettimestampmillis ();
String d = new SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"). Format (new date);
System.out.println ("content:" + body);
System.out.println ("Addresses:" + address);
System.out.println ("Time:" + D);
}
The appropriate channel is configured in the list:
<intent-filter>
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
Example 3:SD card status listener
Get the action.
String action = Intent.getaction ();
if ("Android.intent.action.MEDIA_MOUNTED". Equals (action)) {
SYSTEM.OUT.PRINTLN ("SD card mounted, can be used");
Toast.maketext (Context, "SD card Installed", 0). Show ();
}else if ("Android.intent.action.MEDIA_REMOVED". Equals (action)) {
SYSTEM.OUT.PRINTLN ("SD is unplugged, do not unplug it");
Toast.maketext (Context, "SD was unplugged, do not unplug it", 0). Show ();
}else if ("Android.intent.action.MEDIA_UNMOUNTED". Equals (action)) {
SYSTEM.OUT.PRINTLN ("SD was unloaded, nothing to uninstall it");
Toast.maketext (Context, "SD was unloaded, nothing to uninstall it", 0). Show ();
}
Checklist Configuration:
<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>
Example 4: Uninstall and install monitoring for an application
Get the action.
String action = Intent.getaction ();
System.out.println (action);
if ("Android.intent.action.PACKAGE_ADDED". Equals (action)) {
SYSTEM.OUT.PRINTLN ("Application Installed");
Toast.maketext (Context, "application installed", 0). Show ();
}else if ("Android.intent.action.PACKAGE_REMOVED". Equals (action)) {
SYSTEM.OUT.PRINTLN ("Application uninstalled");
Toast.maketext (Context, "application uninstalled", 0). Show ();
}else if ("Android.intent.action.PACKAGE_REPLACED". Equals (action)) {
SYSTEM.OUT.PRINTLN ("Application overlay Installed");
Toast.maketext (Context, "Application overlay Installed", 0). Show ();
}
The channel configuration inside the list:
<intent-filter>
<action android:name= "Android.intent.action.PACKAGE_ADDED"/>
<action android:name= "Android.intent.action.PACKAGE_REMOVED"/>
<action android:name= "Android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme= "Package"/>
</intent-filter>
This article is from the "Android Notes" blog, so be sure to keep this source http://2585211.blog.51cto.com/10044233/1665711
Apps for Android broadcast