我们正在尝试使用SDK 26作为目标版本来构建我们的自动回复应用。
我们的应用程序中有2个PhoneStateReceivers
(请参见下面的代码)。
另外,我们正在请求权限:
android.permission.READ_SMS
android.permission.READ_PHONE_STATE
并且用户接受它。
但是,在手机重启后和用户解锁设备之前(锁定屏幕),两个SMS广播接收器均无法工作。
相关的AndroidManifest.xml部分:
<receiver android:name="com.lemi.callsautoresponder.callreceiver.DynamicPhoneStateReceiver" android:directBootAware="true" android:enabled="true" android:exported="true" />
<receiver android:name="com.lemi.callsautoresponder.callreceiver.PhoneStateReceiver" android:directBootAware="true" android:enabled="true" android:exported="true">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.provider.Telephony.SMS_DELIVER"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.GSM_SMS_RECEIVED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"/>
<data android:mimeType="application/vnd.wap.sic"/>
</intent-filter>
</receiver>
Application.java的相关部分
public void registerPhoneStateReceiver() {
Log.i(TAG, "register PhoneState receiver");
_phoneReceiver = new DynamicPhoneStateReceiver();
IntentFilter intentFilter1 = new IntentFilter();
//intentFilter.addAction(PhoneStateReceiver.SMS_RECEIVED);
intentFilter1.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
intentFilter1.addAction("android.intent.action.BOOT_COMPLETED");
intentFilter1.setPriority(0x7fffffff);
intentFilter1.addCategory("android.intent.category.DEFAULT");
appContext.registerReceiver(_phoneReceiver, intentFilter1);
IntentFilter intentFilter2 = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
intentFilter2.setPriority(0x7fffffff);
intentFilter2.addCategory("android.intent.category.DEFAULT");
appContext.registerReceiver(_phoneReceiver, intentFilter2, "android.permission.BROADCAST_SMS", null);
IntentFilter intentFilter3 = new IntentFilter("android.provider.Telephony.GSM_SMS_RECEIVED");
intentFilter3.setPriority(0x7fffffff);
intentFilter3.addCategory("android.intent.category.DEFAULT");
appContext.registerReceiver(_phoneReceiver, intentFilter3, "android.permission.BROADCAST_SMS", null);
}
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.info(TAG, "received sms");
}
}
public class DynamicPhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.info(TAG, "received sms");
}
}
最佳答案
AndroidManifest.xml将此行添加到意图过滤器中。
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>