gpt4 book ai didi

Android - 停止 PhoneStateListener/BR/UnregisterReceiver

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:58 25 4
gpt4 key购买 nike

我在停止 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);
}

最佳答案

使用 LISTEN_NONE 作为 listen 方法的参数来停止监听更新。这将使听众听不到任何声音。

telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE );

关于Android - 停止 PhoneStateListener/BR/UnregisterReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16953428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com