BroadcastReceiver适用于早期的Android版本,但更新的不是

时间:2017-01-29 16:56:04

标签: android broadcastreceiver

我遇到了以下问题: 为什么我的CallReceiver适用于较旧的Android版本,而在较新的版本上没有任何反应?是否允许更改权限规则,或者是什么? 有我的代码:

public class CallReceiver extends BroadcastReceiver {

    static boolean isRinging=false;
    static boolean isReceived=false;
    static String callerPhoneNumber;

    @Override
    public void onReceive(Context mContext, Intent intent){

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);


        if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
            isRinging =true;
            Bundle bundle = intent.getExtras();
            callerPhoneNumber= bundle.getString("incoming_number");
        }

        if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
            isReceived=true;
        }

        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
            // detect missed call
            if(isRinging==true && isReceived==false){
                Toast.makeText(mContext, "Missed call from : "+callerPhoneNumber, Toast.LENGTH_LONG).show();
            }
        }
    }
}

清单:

 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".CallReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

根据您的问题,您似乎没有要求更高版本的运行时权限(&gt; = API级别23),因此请通过请求权限启用它们。

有关详情this