gpt4 book ai didi

android - 即使在开机后也能收听电话状态

转载 作者:行者123 更新时间:2023-11-30 02:21:30 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 Broadcast Receiver 监听电话状态事件(即收到调用、空闲等)。代码如下:

public class PhoneCallreceiver extends BroadcastReceiver {
private boolean ringing;
private boolean received;

public PhoneCallreceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
Log.v("ranjith", "enterted receiver");
Bundle bundle = intent.getExtras();
String incomingnumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.v("ranjapp", state);

if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
ringing = true;
received = false;
}
}

list .xml

    <receiver
android:name=".PhoneCallreceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>

我的问题是我想在设备重新启动后自动启动BroadCast Receiver。我有一个 BroadCast Receiver 来收听启动,但我不确定如何启动 PhoneCallreceiver BroadCast 接收器。

list .xml

   <receiver
android:name=".backend.BootStarter"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

代码:

public class BootStarter extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
PhoneCallreceiver callreceiver = new PhoneCallreceiver();
context.registerReceiver(callreceiver,new IntentFilter(**what to put here**));
}
}

最佳答案

找到我问题的答案,接收电话状态的 intentfilter 操作是 android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED

我的更新代码如下:

public class BootStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PhoneCallreceiver callreceiver = new PhoneCallreceiver();
context.registerReceiver(new PhoneCallreceiver(), new IntentFilter(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
}

关于android - 即使在开机后也能收听电话状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28463324/

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