2013-06-06 21 views
2

我有問題停止PhoneStateListener。我試圖使用unregisterReceiver(),但它不起作用。我看過網站,發現我必須使用LISTEN.NON,但我無法做到。
Android - 停止PhoneStateListener/BR/UnregisterReceiver

我的問題有以下幾種:
1 - 我在哪裏已經把我的 「telephony.listen(聽衆,PhoneStateListener.LISTEN_NONE);」在我的代碼?
2 - 我必須使用unregisterReceiver嗎?

我的代碼不容易解釋,而且相當長,所以我不會給我所有的代碼,只是主視圖。

事實上:我從計時器開始一項服務。該服務的註冊BR

/*第一個*/

public class TmrSettingsCheck extends BroadcastReceiver 
{ 
    public void setTimer(long milli) 
    { // setRepeating } 

    public void onReceive(Context context, Intent intent)   
    { 
    // Try to know if we have to start or stop the service 
    context.startService(new Intent(context, SvcCall.class)); 
    OR 
    context.stopService(new Intent(context, SvcCall.class)); 
    } 
} 

/*第二個*/

public class SvcCall extends Service 
{ 
    private BroadcastReceiver br_call; 

    public void onCreate() 
    { // Creation } 

    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
    // register the broadcast receiver 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(ACTION_OUT); 
    filter.addAction(ACTION_IN); 
    this.br_call = new CallBR(this._context); 
    this.registerReceiver(this.br_call, filter); 
    return (START_STICKY); 
    } 

    public class CallBR extends ClsCallReceiver 
    { 
    public CallBR(Context context) 
    { // constructor } 

    protected void onIncomingCallStarted(String number, Date start) 
    { // XYZ 
    } 

    protected void onIncomingCallEnded(String number, Date start, Date end) 
    { // XYZ } 

    protected void onOutgoingCallStarted(String number, Date start) 
    { // XYZ } 

    protected void onOutgoingCallEnded(String number, Date start, Date end) 
    { // XYZ } 

    protected void onMissedCall(String number, Date start) 
    { // XYZ } 
    } 

    public IBinder onBind(Intent intent) 
    { return null; } 

    public void onDestroy() 
    { 
    this.unregisterReceiver(this.br_call);   
    super.onDestroy(); 
    } 
} 

/*第三個*/

public abstract class ClsCallReceiver extends BroadcastReceiver 
{ 
    public static PhonecallStartEndDetector listener; 
    String outgoingSavedNumber; 
    protected Context savedContext; 
    TelephonyManager telephony; 

    public void onReceive(Context context, Intent intent) 
    { 
    savedContext = context; 
    if (listener == null) 
    listener = new PhonecallStartEndDetector(); 

    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) 
    { 
     listener.setOutgoingNumber(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER")); 
     return; 
    } 
    this.telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    this.telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); 
    } 

    public class PhonecallStartEndDetector extends PhoneStateListener 
    { 
    public PhonecallStartEndDetector() 
    { // XYZ } 

    public void onCallStateChanged(int state, String incomingNumber) 
    { // XYZ }  
    } 

    protected abstract void onIncomingCallStarted(String number, Date start); 
    protected abstract void onOutgoingCallStarted(String number, Date start); 
    protected abstract void onIncomingCallEnded(String number, Date start, Date end); 
    protected abstract void onOutgoingCallEnded(String number, Date start, Date end); 
    protected abstract void onMissedCall(String number, Date start); 
} 
+0

我不知道你的具體問題,但我聽說你應該使用try漁獲unregisterreciever因爲如果reciever未註冊您的應用程序會崩潰註銷未註冊 – JRowan

+0

一個reciever @ JRowan:我已經嘗試過使用try/finally塊。仍然是相同的結果:PhoneStateListener仍在監聽。我發現我必須使用PhoneStateListener.LISTEN_NONE,但我找不到一個方法(相對於我以前的代碼/文章)。 – frontal

+0

你有多個廣播接收機運行,也許你必須以特定的方式實例化它們,唯一的註冊和取消註冊,也許只是註冊一般broadcastreciever正在啓動所有的broadcastrecievers並失去取消註冊,我只使用1廣播接收器for proximityalerts,對不起,如果我在浪費你的時間,idk – JRowan

回答

3

使用LISTEN_NONE作爲聽方法停止偵聽更新的參數。這將使聽者不聽。

telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);