小编mau*_*uza的帖子

为什么这个代码不适用于android 6 marshmallow Api 23?

为什么这个代码不适用于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);
Run Code Online (Sandbox Code Playgroud)

权限:

<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" /> …
Run Code Online (Sandbox Code Playgroud)

android telephonymanager android-6.0-marshmallow

4
推荐指数
1
解决办法
1万
查看次数