2013-04-15 8 views
8

ciao voglio registrare il numero in arrivo nel database. sto usando il ricevitore broadcast per ascoltare le chiamate telefoniche e l'ascolto del telefono. Ecco il mio codiceListener dello stato del telefono chiamato più volte

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> 

in ThePhoneStateListener onCallStateChanged() il motivo per cui viene chiamato più volte . Voglio inserire il numero in entrata nel database solo una volta.

+0

cosa è l'uso di callactionlistener – stinepike

risposta

28

Alcuni come, l'ho risolto. il problema è che quando è stato invocato il mio ricevitore broadcast suReceive(), ogni volta cercavo di ascoltare un nuovo PhoneStateListener. Devi solo farlo una volta. proprio come seguendo

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); 
     } 


    } 

} 

Problema risolto. Cheers

+1

grande soluzione con questo uso variabile statica. –

+0

Grazie! Funziona alla grande! –

3

Grazie per la soluzione, ma ha funzionato per me quando ho implementato l'oggetto singleton della mia classe PhoneStateListener. onReceive nel mio BroadcastReceiver:

@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); 
    } 
} 

e questo è il metodo nella mia classe PhoneStateListener:

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

    return callStateInstance; 
} 

Spero che sarà utile per qualcuno.

0

Sto semplicemente creo variabile statica e controllo

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 = "..."; 
     } 
    } 
Problemi correlati