我知道这个问题已经在网站上被问了很多,但是,我似乎无法找到解决方案.当应用程序未运行时,不会调用我的BOOT_COMPLETED接收器.
表现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.startuptest"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.startuptest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.startuptest.StartUpBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
StartUpBootReceiver:
public class StartUpBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("startuptest", "StartUpBootReceiver " + intent.getAction());
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果应用程序正在运行,我用模拟呼叫
adb shell
am broadcast -a …Run Code Online (Sandbox Code Playgroud) 如果应用程序在前台,后台运行或被杀死,该应用程序会显示预期的行为.但是,一旦重新启动,PeriodicTask停止运行
以下是相关的代码:
在AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".tracking.MyTaskService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter>
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
PeriodicTask配置:
PeriodicTask task = new PeriodicTask.Builder()
.setService(MyTaskService.class)
.setTag(TASK_TAG_PERIODIC)
.setPeriod(30L)
.setFlex(10L)
.setExtras(bundle)
.setPersisted(true)
.build();
mGcmNetworkManager.schedule(task);
Run Code Online (Sandbox Code Playgroud)
在Logcat中,我得到以下内容:
E/NetworkScheduler.TED: Couldn't start service: Intent
{ act=com.google.android.gms.gcm.ACTION_TASK_READY
cmp=xxx.xxxxxx.xxx/.tracking.MyTaskService (has extras)
}
Run Code Online (Sandbox Code Playgroud)
附加所有相关细节:
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.example.gcmnetworkmanagerquickstart">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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>
<!-- [START manifest_service] -->
<service
android:name=".MyTaskService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE"> …Run Code Online (Sandbox Code Playgroud) android google-cloud-messaging periodic-task gcm-network-manager
我正在处理的应用程序允许用户允许应用程序读取确认SMS的内容以自行输入验证码.对于使用早于Oreo(API 26)的OS的所有设备,BroadcastReceiver的实现正常工作并允许正确接收SMS.通过这个实现,我的意思是将接收器对象放在AndroidManifest中.
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
但是,从Oreo开始,必须将BroadcastReceivers显式注册到适当的上下文.我已经实现了如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
smsReceiver = new SmsReceiver();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION);
this.registerReceiver(smsReceiver, intentFilter);
}
Run Code Online (Sandbox Code Playgroud)
在接收Manifest.permission.READ_SMS的许可时执行此代码块.SmsReceiver类扩展了BroadcastReceiver并覆盖了它的onReceive()方法.
在这里,我有几个问题:
我测试了这个实现,并在我的SmsReceiver中的onReceive()方法上设置了断点.当SMS到达时,应用程序永远不会进入onReceive()方法.为什么会这样?
我按照Android Developer网站上描述的方式实例化我的IntentFilter,即使用ConnectivityManager.CONNECTIVITY_ACTION操作.我知道SmsReceiver有效,因为onReceive()中的断点总是在接收器注册时被击中.但是,该操作仅仅是CONNECTIVITY_ACTION.SMS_RECEIVED_ACTION永远不会被接收器捕获.是否绝对有必要使用此操作实例化IntentFilter,还是可以将其删除?
还有其他我想念的东西会导致我的接收器没有收到到达的短信吗?
android broadcastreceiver android-broadcastreceiver android-8.0-oreo
android ×3