0

Initially I had Android 7.0 and didn't have any issues using a BroadcastReceiver and service. However with changes to Android 8.0. I needed to switch to a JobIntentService so my application can run on bootup.

I have tried migrating my code to match the JobIntentService but nothing is happening on bootup. I am unsure whether the reason is because of my service class or my BroadcastReceiver class.

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");
        }
    }

So I am trying to essentially start the Home.class on bootup.

Rockey
  • 393
  • 4
  • 18
ScuffedCoder
  • 376
  • 1
  • 5
  • 21
  • Take a loot at https://stackoverflow.com/questions/6391902/how-do-i-start-my-app-on-startup – Rockey Jan 02 '19 at 14:44
  • This doesn't help because with android 8.0 you cannot use implicit broadcast receivers so I am trying to use a job intent service. The link doesn't provide any sort of help? – ScuffedCoder Jan 02 '19 at 14:47
  • I think `ACTION_BOOT_COMPLETED` is still accepted on Android 8 https://stackoverflow.com/questions/45981888/registering-boot-completed-receiver-in-android-8 – Rockey Jan 02 '19 at 14:52
  • Well..I have included this in my manifest as well. Still no luck. Not sure what the issue is. – ScuffedCoder Jan 02 '19 at 15:07

1 Answers1

0

I tried it and it could run normally. You could check three tips below.

1.Check whether you have declared RECEIVE_BOOT_COMPLETED permission or not.

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

2.Check whether you have declared the receiver with BOOT_COMPLETED action.

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

3.Remove Toast.makeText(this, "Application and Service Started", Toast.LENGTH_LONG).show(); in your service or toast it in main thread. Otherwise it gives you the error java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare().

wolfhan
  • 1
  • 2