gpt4 book ai didi

android - PhoneStateListener 不适用于三星 GT-S7562

转载 作者:太空狗 更新时间:2023-10-29 15:41:51 25 4
gpt4 key购买 nike

我正在三星 GT-S7562(双卡 4.0)上测试 PhoneStateListener,然后 STATE 总是返回 0,INCOMING NUMBER 总是返回空。虽然它在我的另一台设备 SAMSUNG GT-S5570(single sim-2.2)上运行良好。这是我的代码。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class IncomingCall extends BroadcastReceiver {
Context pcontext;

public void onReceive(Context context, Intent intent) {
pcontext = context;

try {
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);

MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}

}

private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(
pcontext,
"CALL_STATE_IDLE.\nIncomming Number : "
+ incomingNumber, Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(
pcontext,
"CALL_STATE_OFFHOOK.\nIncomming Number : "
+ incomingNumber, Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(
pcontext,
"CALL_STATE_RINGING.\nIncomming Number : "
+ incomingNumber, Toast.LENGTH_SHORT).show();
break;
default: {
Toast.makeText(pcontext,
"Default.\nIncomming Number : " + incomingNumber,
Toast.LENGTH_SHORT).show();
}
}
}
}
}

还在 list 文件中添加了权限。

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

<receiver android:name="com.example.caller.ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

最佳答案

PhoneStateListener 不能在很多设备上工作,尤其是在华为和中兴上。你可以试试这个:

public class PhoneStateReceiver extends BroadcastReceiver {
private static final String TAG = PhoneStateReceiver.class.getSimpleName();
private static String incoming_number = null;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); // outgoing call
Log.i(TAG, "call OUT:" + phoneNumber);
} else {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);

switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING: // incoming call
incomingFlag = true;
incoming_number = intent.getStringExtra("incoming_number");
LogUtils.d(TAG, "RINGING :" + incoming_number);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (incomingFlag) {
LogUtils.d(TAG, "incoming ACCEPT :" + incoming_number);
}
break;

case TelephonyManager.CALL_STATE_IDLE:
if (incomingFlag) { // hang up

LogUtils.d(TAG, "incoming IDLE, number:" + incoming_number);
}
break;
}
}
}

list :

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

但是我不确定这个广播是否总是有效。我同时使用这两种方式。它工作正常。祝你好运。

关于android - PhoneStateListener 不适用于三星 GT-S7562,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20930684/

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