2013-04-15 47 views
8

您好我想將傳入號碼記錄到數據庫。我正在使用廣播接收機來收聽電話和使用電話狀態監聽器。 這裏是我的代碼多次調用電話狀態偵聽器

ThePhoneStateListener.java

package com.example.netlogger.Receiver; 
import android.content.Context; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 

public class ThePhoneStateListener extends PhoneStateListener { 
    Context context; 
    public ThePhoneStateListener(Context context) { 
     this.context = context; 
    } 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     super.onCallStateChanged(state, incomingNumber); 
     if (state == TelephonyManager.CALL_STATE_OFFHOOK) { 
      Log.d("TAGG", "Insert: " + incomingNumber); 
     } 

    } 
} 

CallActionReceiver.java

package com.example.netlogger.Receiver; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

public class CallActionsReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     TelephonyManager manager = (TelephonyManager) arg0 
       .getSystemService(arg0.TELEPHONY_SERVICE); 
     manager.listen(new ThePhoneStateListener(arg0), 
       android.telephony.PhoneStateListener.LISTEN_CALL_STATE); 
    } 
} 

Menifest.xml

<receiver android:name="com.example.netlogger.Receiver.CallActionsReceiver"> 
    <intent-filter android:priority="2147483647"> 
     <action android:name="android.intent.action.PHONE_STATE"/> 
    </intent-filter> 
    <intent-filter android:priority="2147483647"> 
     <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> 
    </intent-filter> 
</receiver> 

ThePhoneStateListener onCallStateChanged()爲什麼它被稱爲多次。我想只將一次傳入的數字插入數據庫。

+0

這有什麼靜態變量使用使用callactionlistener – stinepike

回答

28

一些如何,我解決了它。問題是,當我的廣播接收器的onReceive()被調用時,每當我試圖聽一個新的PhoneStateListener.You只需要做一次。就像下面的

package com.example.netlogger.Receiver; 

import java.util.Date; 

import com.example.netlogger.util.LocalDatabase; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

public class CallActionsReceiver extends BroadcastReceiver { 
    static ThePhoneStateListener phoneStateListener; 



    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     TelephonyManager manager = (TelephonyManager) arg0 
       .getSystemService(arg0.TELEPHONY_SERVICE);  
     if (phoneStateListener == null) { 
      phoneStateListener = new ThePhoneStateListener(arg0); 
      manager.listen(phoneStateListener, 
        android.telephony.PhoneStateListener.LISTEN_CALL_STATE); 
     } 


    } 

} 

問題解決了。歡呼聲

+1

的最佳解決方案。 –

+0

謝謝!很棒! –

3

謝謝你的解決方案,但它對我有用,當我實現了我的PhoneStateListener類的單例對象。 onReceive在我的廣播接收器:

@Override 
public void onReceive(final Context context, Intent intent) { 
    telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    if (phoneListener == null) { 
     phoneListener = CallStateListener.getCallStateInstance(context);CallStateListener(context); 
     telephony.listen(phoneListener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE); 
    } 
} 

,這是方法我PhoneStateListener類:

public static CallStateListener getCallStateInstance(Context ctx) { 
    if (callStateInstance == null) 
     callStateInstance = new CallStateListener(ctx); 

    return callStateInstance; 
} 

希望這將是爲別人有用。

0

我只是創建靜態變量和校驗

public class IncomingCallReceiver extends BroadcastReceiver 
{ 
    private static String canEndCall; 

    @Override 
    public void onReceive(final Context context, Intent intent) 
    { 
     if (canEndCall == null) 
     { 
      TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
      tmgr.listen(new PhoneStateListener() 
      { 
       @Override 
       public void onCallStateChanged(int state, String incomingNumber) 
       { 

       } 
      }, PhoneStateListener.LISTEN_CALL_STATE); 
      canEndCall = "..."; 
     } 
    }