gpt4 book ai didi

android - addAction 到 IntentFilter 以处理电话状态更改

转载 作者:行者123 更新时间:2023-11-29 19:20:28 27 4
gpt4 key购买 nike

我想接听来电。

我知道您必须在 AndroidManifest 中授予权限并设置接收器

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

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


但是,在我的项目中,我通过代码设置了接收器,

IntentFilter filter = new IntentFilter();
filter.addAction( ... );
registerReceiver(broadcastReceiver, filter);

addAction 里面我放了 TelephonyManager.ACTION_PHONE_STATE_CHANGED
然后我用

创建了BroadcastReceiver
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { ...

然后,在其中,我使用 String action = intent.getAction(); 从 Intent 中获取操作,然后检查它是哪个操作。

它非常适合蓝牙

filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);

switch (action) {
case BluetoothAdapter.ACTION_DISCOVERY_STARTED: ...

但它不检查 TelephonyManager.ACTION_PHONE_STATE_CHANGED

那么,我的问题是:
如何设置 Action (addAction())来监听状态变化?
TelephonyManager.ACTION_PHONE_STATE_CHANGED不对吗?
如果我放弃并在 AndroiManifest 中设置接收器,我将如何设置蓝牙过滤器?我正在使用

BluetoothDevice.ACTION_FOUND
BluetoothAdapter.ACTION_DISCOVERY_STARTED
BluetoothAdapter.ACTION_DISCOVERY_FINISHED

最佳答案

首先在你的 Activity 中注册接收者

this.registerReceiver(this.broadcastReceiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));

这是广播接收器:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

if (state == null) {

//Outgoing call
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.e("tag", "Outgoing number : " + number);

} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

Log.e("tag", "EXTRA_STATE_OFFHOOK");

} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

Log.e("tag", "EXTRA_STATE_IDLE");

} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e("tag", "Incoming number : " + number);

} else
Log.e("tag", "none");
}
};

关于android - addAction 到 IntentFilter 以处理电话状态更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616849/

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