我遇到了以下问题: 为什么我的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>
感谢您的帮助