爲什麼不能在android 6棉花糖Api 23上工作?它不會拋出異常,但callStateListener中的代碼不起作用。爲什麼不能在android 6棉花糖Api 23上工作?
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',然後不做任何事情。 – CommonsWare
Android不具備android.permission.PROCESS_INCOMING_CALLS權限現在,它使用的是較早出現,檢查權限 – AshuKingSharma