为什么这段代码不能在 android 6 marshmallow Api 23 上运行?它不会抛出异常,但 callStateListener 中的代码不起作用。
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
//if(logAtive) Log.i(LOG_TAG,incomingNumber + " " + state);
if(state==TelephonyManager.CALL_STATE_RINGING){
Toast.makeText(getApplicationContext(),"Hey, receive your call. Phone is ringing.",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
Toast.makeText(getApplicationContext(),"You are in a call. ",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
Toast.makeText(getApplicationContext(),"You are in idle state… ",
Toast.LENGTH_LONG).show();
}
}
};
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
权限:
<uses-feature android:name="android.hardware.telephony" android:required="true" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
它在 Android 5.1.1 中运行良好,但在 6(API 级别 23)中运行不佳
最佳答案
我刚刚遇到这个问题并弄明白了。这是因为您的目标是 android SDK 23。如果您将 list 文件更改为目标 API 22,那么即使在 Marshmallow 设备上安装并运行它也会开始工作。
如果您仍想以 API 23 为目标,那么您必须先使用新的运行时权限 API 来请求权限使用,否则将被拒绝。
https://developer.android.com/training/permissions/requesting.html
关于android - 为什么此代码不适用于 android 6 marshmallow Api 23?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32827081/