我们正在尝试使用 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 将此行添加到 Intent 过滤器中。
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
关于java - 手机重启后短信广播接收器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248282/