android - 为什么此代码不适用于 android 6 marshmallow Api 23?

标签 android telephonymanager android-6.0-marshmallow

为什么这段代码不能在 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/

相关文章:

java - 电话经理的值(value)观是否可靠且多变(cdma)?

android - 在 Android 6/7 中为 TCP 连接指定承载

android - 如何使用JNI android获取应用程序包名称或applicationId

java - 程序类型已存在 : com. google.common.annotations.GwtCompatible

android - 不使用 ACTION_PHONE_STATE_CHANGED 获取当前来电者电话号码

android - android TelephonyManager 是否缓冲 GSMCellILocation?

android - 在 Marshmallow 及以上版本中使用 SyncAdapter 进行同步的问题

android - RecyclerView onScrollChangeListener

android - 我怎样才能得到平的时间?

Android WheelView 是全黑的?