【问题标题】:Android 8.0 - Job Intent Service does not run application on bootupAndroid 8.0 - Job Intent Service 在启动时不运行应用程序
【发布时间】:2019-01-02 14:42:04
【问题描述】:

最初我使用的是 Android 7.0,使用 BroadcastReceiver 和服务没有任何问题。但是随着 Android 8.0 的变化。我需要切换到JobIntentService,这样我的应用程序才能在启动时运行。

我已尝试迁移我的代码以匹配 JobIntentService,但启动时没有任何反应。 我不确定原因是因为我的服务类还是我的BroadcastReceiver 类。

AndroidManifest.xml

    <service android:name=".backgroundService"
                android:permission="android.permission.BIND_JOB_SERVICE"/>

backgroundService.java

    public class backgroundService extends JobIntentService {

        public static final int JOB_ID = 0x01;

        public static void enqueueWork(Context context, Intent work) {
            enqueueWork(context, backgroundService.class, JOB_ID, work);
        }


        @Override
        protected void onHandleWork(@NonNull Intent intent) {

            Toast.makeText(this, "Application and Service Started", Toast.LENGTH_LONG).show();
            Intent dialogIntent = new Intent(this, Home.class);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(dialogIntent);
        }
    }

startOnBoot.java

    public class startOnBoot extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                Log.i("In" , "getAction() - Boot");
                backgroundService.enqueueWork(context, intent);
            }
            else
                Log.i("No" , "Boot");
        }
    }

所以我试图在启动时启动Home.class

【问题讨论】:

  • 这无济于事,因为在 android 8.0 中你不能使用隐式广播接收器,所以我正在尝试使用工作意图服务。该链接没有提供任何帮助?
  • 我认为 ACTION_BOOT_COMPLETED 在 Android 8 上仍然被接受 stackoverflow.com/questions/45981888/…
  • 嗯..我也将它包含在我的清单中。仍然没有运气。不知道是什么问题。

标签: java android broadcastreceiver jobintentservice


【解决方案1】:

我试过了,它可以正常运行。您可以查看以下三个提示。

1.检查你是否声明了RECEIVE_BOOT_COMPLETED权限。

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

2.检查你是否已经用BOOT_COMPLETED action 声明了receiver。

<receiver android:name=".startOnBoot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

3.在您的服务中删除Toast.makeText(this, "Application and Service Started", Toast.LENGTH_LONG).show(); 或在主线程中对其进行烘烤。否则它会给你错误java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多