手机重启后,短信广播接收器不起作用

时间:2018-01-14 09:37:41

标签: java android android-intent

我们正在尝试使用SDK 26构建我们的自动回复应用程序作为目标版本。

我们的应用中有2个PhoneStateReceivers(请参阅下面的代码)。

我们也要求权限:

android.permission.READ_SMS
android.permission.READ_PHONE_STATE 

并且用户接受它。

但是手机重启后,用户解锁设备(锁定屏幕)之前,两个短信广播接收器都无法正常工作。

相关的 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");

  }
}

2 个答案:

答案 0 :(得分:0)

您必须在Predicate p = new DatePredicate(new BirthId("12345",passSpecifiedDate())); Result res = imap.values(p); 文件中添加以下权限。

AndroidManifest.xml

您必须通过两个操作注册接收器以完成启动。

牛轧糖和上层设备

LOCKED_BOOT_COMPLETED 操作。

<强>>牛轧糖设备

BOOT_COMPLETED 操作。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

手机重新启动时,此<receiver android:name=".BootCompletedReceiver"> <intent-filter> <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> 将会触发。在此BootCompletedReceiver课程中,您必须以编程方式注册所有广播接收器。这样,所有的广播接收器就开始工作了。

答案 1 :(得分:0)

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>