Android broadcast mechanism-broadcast

Source: Internet
Author: User

Http://www.cnblogs.com/TerryBlog/archive/2010/08/16/1801016.html

In Android, if we want to accept broadcast information, we have to implement this broadcast receiver by ourselves. We can inherit from it.BroadcastreceiverYou can have a receiver. There is not enough receiver. We have to rewrite it.BroadcastreceiverInsideOnreceiverMethod: What should we do when we come to broadcast? We need to implement it ourselves, but we can build an information firewall. SpecificCode:

Public   Class Smsbroadcastreceiver Extends Broadcastreceiver
{

@ Override
Public   Void Onreceive (context, intent)
{
Bundle bundle = Intent. getextras ();
Object [] Object = (Object []) bundle. Get ( " PDUS " );
Smsmessage SMS [] = New Smsmessage [object. Length];
For ( Int I = 0 ; I < Object. length; I ++ )
{
SMS [ 0 ] = Smsmessage. createfrompdu (( Byte []) Object [I]);
Toast. maketext (context, " From " + SMS [I]. getdisplayoriginatingaddress () + " The message is: " + SMS [I]. getdisplaymessagebody (), Toast. length_short). Show ();
}
// Terminate broadcast. Here we can handle it a little, and implement SMS Firewall Based on the Number entered by the user.
Abortbroadcast ();
}

}

 

When the broadcast receiver is implemented, you also need to set the type of information that the broadcast receiver receives. Here is the information:Android. provider. telephony. sms_received

We can register the broadcast receiver in the system to let the system know that we have a broadcast receiver. There are two types: dynamic code registration:

// Generate broadcast Processing
Smsbroadcastreceiver =   New Smsbroadcastreceiver ();
// Instantiate the filter and set the broadcast to be filtered
Intentfilter = new intentfilter ("android. provider. telephony. sms_received ");

//Register Broadcast
Broadcastreceiveractivity.This. Registerreceiver (smsbroadcastreceiver, intentfilter );

 

One is inAndroidmanifest. xmlConfigure Broadcast

<? XML version = "1.0" encoding = "UTF-8" ?>
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "SPL. broadcastreceiver"
Android: versioncode = "1"
Android: versionname = "1.0" >
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Activity Android: Name = ". Broadcastreceiveractivity"
Android: Label = "@ String/app_name" >
< Intent-Filter >
< Action Android: Name = "Android. Intent. Action. Main"   />
< Category Android: Name = "Android. Intent. Category. launcher"   />
</ Intent-Filter >
</ Activity >

<! -- Broadcast Registration -->
< Cycler Android: Name = ". Smsbroadcastreceiver" >
< Intent-Filter Android: Priority = "20" >
< Action Android: Name = "Android. provider. telephony. sms_received" />
</ Intent-Filter >
</ Cycler >

</ Application >

< Uses-SDK Android: minsdkversion = "7"   />

<! -- Permission Application -->
< Uses-Permission Android: Name = "Android. Permission. receive_sms" > </ Uses-Permission >

</ Manifest >

 

 

The differences between the two registration types are:

1) the first type is not resident broadcast, that is, broadcast followsProgram.

2) The second type is the resident type. That is to say, when the application is closed, if information is broadcast, the program will be automatically run by the system call.

 

Related Article

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.