我在停止 PhoneStateListener 时遇到问题。我尝试使用 unregisterReceiver() 但它没有用。我确实阅读了该站点并看到我必须使用 LISTEN.NON 但我无法做到。
我的问题如下:
1 - 我必须把我的“telephony.listen(listener, 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);
}