[Reprinted] The Listener is canceled after the program is disabled in Android.

Source: Internet
Author: User
The listener that receives text messages will always be in the background, even if Program The listener will always exist and activate the main program when receiving the message. In general design, this is not the case because it is a waste of resources and does not provide a good user experience. Therefore, you need to cancel the listener after the program is closed. Previous Link (click to enter)

1. Create a backend service to receive messages and broadcast the messages to broadcastreceiver. Because the service can be terminated, messages will not be forwarded when the service is terminated, and the broadcastreceiver in the background will not take effect and will be automatically recycled by GC. The program's goal has also been achieved

Package com. Demo;
Import Android. App. Service;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. intentfilter;
Import Android. OS. Bundle;
Import Android. OS. ibinder;
Import Android. telephony. GSM. smsmessage;

Public class listenservice extends Service {
Public static final string svrid = "rarnu. Service. Demo ";
Public static final string stract = "android. provider. telephony. sms_received ";
Private smsmsgreceiver Recv;
@ Override
Public void onstart (intent, int startid ){
Super. onstart (intent, startid );
}
@ Override
Public void oncreate (){
Intentfilter filter = new intentfilter (stract );
Recv = new smsmsgreceiver ();
Registerreceiver (Recv, filter );
Super. oncreate ();
}
@ Override
Public ibinder onbind (intent ){
Return NULL;
}
@ Override
Public void ondestroy (){
Unregisterreceiver (Recv );
Super. ondestroy ();
}
Public class smsmsgreceiver extends broadcastreceiver {
@ Override
Public void onreceive (context, intent ){
If (intent. getaction (). Equals (stract )){
Stringbuilder sb = new stringbuilder ();
Bundle bundle = intent. getextras ();
If (bundle! = NULL ){
Object [] PDUS = (object []) bundle. Get ("PDUS ");
Smsmessage [] MSG = new smsmessage [PDUS. Length];
For (INT I = 0; I <PDUS. length; I ++ ){
MSG [I] = smsmessage. createfrompdu (byte []) PDUS [I]);
}
For (smsmessage currmsg: MSG ){
SB. append ("from :");
SB. append (currmsg. getdisplayoriginatingaddress ());
SB. append ("\ nmessage :");
SB. append (currmsg. getdisplaymessagebody ());
}
}
Intent I = new intent (svrid );
I. putextra ("Param", SB. tostring ());
Sendbroadcast (I );
}
}
}
}

You can see thatCodeThe only difference between the original and unblocked copies to the Service Code is that sendbroadcast () is added to the service ()

Ii. Modify broadcastreceiver
Package com. Demo;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. widget. Toast;

Public class messagereceiver extends broadcastreceiver {
Public static final string svrid = "rarnu. Service. Demo ";
@ Override
Public void onreceive (context, intent ){
If (intent. getaction (). tostring (). Equals (svrid )){
Bundle Bund = intent. getextras ();
String strparam = "";
If (Bund! = NULL ){
Strparam = Bund. getstring ("Param ");
}
Intent I = new intent (context, msglisten. Class );
I. setflags (intent. flag_activity_new_task );
Toast. maketext (context, strparam, Toast. length_short). Show ();
Context. startactivity (I );
}
}
}

The text message receiving code is moved to the service, so broadcastreceive naturally does not need that code and can directly retrieve the service string from extras.

Iii. start and end
Package com. Demo;
Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;

Public class msglisten extends activity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
@ Override
Protected void onresume (){
Intent I = new intent (msglisten. This, listenservice. Class );
I. setflags (intent. flag_activity_new_task );
Startservice (I );
Super. onresume ();
}
@ Override
Protected void onpause (){
Intent I = new intent (msglisten. This, listenservice. Class );
Stopservice (I );
Super. onpause ();
}
}

Start the service in the onresume () event of the program and stop the service in the onpause () event.

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.