android - How to make sure the Broadcast Receiver is disconnected -


i wrote simple broadcast receiver catches incoming calls , starts activity caller's number:

package com.example.nrsearch;  import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.phonestatelistener; import android.telephony.telephonymanager; import android.util.log;  public class callreceiver extends broadcastreceiver { public callreceiver() { }  public context context;  @override public void onreceive(context context, intent intent) {     log.i("callreceiverbroadcast", "onreceive() called. ");     this.context = context;     telephonymanager telemgr = (telephonymanager)context.getsystemservice(context.telephony_service);     phonestatelistener psl = new phonestatelistener() {         @override         public void oncallstatechanged(int state, string incomingnumber) {             log.i("callreceiverbroadcast", "oncallstatechanged() called. ");             switch (state) {             case telephonymanager.call_state_ringing:                 log.i("callreceiverbroadcast", "incoming call caught. caller's number " + incomingnumber + ".");                 startnumberdisplayactivity(incomingnumber);             }         }     };     telemgr.listen(psl, phonestatelistener.listen_call_state);     telemgr.listen(psl, phonestatelistener.listen_none); }  public void startnumberdisplayactivity(string incomingnumber) {     intent = new intent(context, mainactivity.class);     i.setflags(intent.flag_activity_new_task);     i.putextra("incomingnumber", incomingnumber);     context.startactivity(i); } } 

however, after first incoming call feel device's battery starts drain pretty quickly. i'm afraid process still prevents broadcast receiver disconnecting (i mean broadcast receiver may running forever). so, there in code cause such behavior , line stops telephonymanager listening call state changes: telemgr.listen(psl, phonestatelistener.listen_none); or should other way?

edit: i'm sure class causes battery drain, because i'm testing battery life app uninstalled , it's lower previous when app installed , broadcast receiver called. can't swear class cause of drain battery consumption difference visible , without app. @ code , cause battety drain? in advance!

in method

@override  public void onpause() {  super.onpause(); mactivity.unregisterreceiver(myreceiver);  } 

you can put other places, that's one. registration in onresume.

also please read broadcastreceiver docs, don't work way seem believe do:

http://developer.android.com/reference/android/content/broadcastreceiver.html

basically receiver lifecycle is:

receiver lifecycle

a broadcastreceiver object valid duration of call onreceive(context, intent). once code returns function, system considers object finished , no longer active.

this has important repercussions can in onreceive(context, intent) implementation: requires asynchronous operation not available, because need return function handle asynchronous operation, @ point broadcastreceiver no longer active , system free kill process before asynchronous operation completes.

in particular, may not show dialog or bind service within broadcastreceiver. former, should instead use notificationmanager api. latter, can use context.startservice() send command service.

edit:

as per comments - concern broadcastreceiver eating battery life isn't real. receiver lasts long takes run code in it's on receiver method. @ point android clean deems nescesary. if in code breaking be:

this.context = context;

/// these 3 lines     telephonymanager telemgr = (telephonymanager)context.getsystemservice(context.telephony_service);    .....     telemgr.listen(psl, phonestatelistener.listen_call_state); telemgr.listen(psl, phonestatelistener.listen_none); 

since create object , try listen on it.

however should read docs:

anything requires asynchronous operation not available,

which doing in code - attaching service , and waiting asyncronous response. isn't acceptable in broadcastreceiver, , indicated in docs @ point:

in particular, may not show dialog or bind service within broadcastreceiver.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -