【发布时间】:2015-09-28 15:54:30
【问题描述】:
为什么这段代码不能在 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)中无法正常运行
【问题讨论】:
-
可能是因为新的权限模型
-
该代码不执行任何操作。你创建了一个
PhoneStateListener,然后什么都不做。 -
Android现在没有android.permission.PROCESS_INCOMING_CALLS权限,以前是有的,检查权限
标签: android telephonymanager android-6.0-marshmallow