3

I have a program with a service and if I reboot my device also the service should Restart. But this only works on the Emulator if I try this on my real device the service doesn't start at all.

Does someone know what I'm doing wrong or why it only works on Emulator?

BroadcastReviever:

@Override
public void onReceive(Context context, Intent intent) {
    //we double check here for only boot complete event
    if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
    {
        //here we start the service
        Intent serviceIntent = new Intent(context, UploadService.class);
        context.startService(serviceIntent);
    }
}

Manifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS_PRIVILEGED"
    tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Upload FTP"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver
        android:name=".BootCompletedIntentReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>


    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter  >
    </activity>

    <service
        android:name=".UploadService"
        android:isolatedProcess="false"
        android:exported="true"
        android:enabled="true"/>
</application>

The emulator is at API 23 and real device is API 27. I'm Building for a min API Level 23 and max API Level 27

EDIT

Now I have also tried the program with a emulator and android API 27 and there when I start my program and then I restart the emulator, the emulator doesn't start any more. As soon as the emulaor has started he begins to reboot again and that in a endless loop. (Real device starts normal just doesn't restarts service)

user12346352
  • 176
  • 2
  • 20
  • 1
    You probably want to share the Android API level(s) you support, and how the emulator and real device differ in that regard. What debugging have you done? Is Your receiver even being invoked? –  Jul 11 '18 at 14:00
  • Emulators API = 23 and real devices API = 27 I hope that does not make much difference – user12346352 Jul 11 '18 at 14:03
  • 1
    [Edit] the question and provide details like that in the question body. Comments can be removed because SO is not a threaded forum. You should also tell us what min and max API level you are building for. –  Jul 11 '18 at 14:04
  • Do you Need something else about my programm – user12346352 Jul 11 '18 at 14:10
  • You should also tell us what research and debugging you have done. Debugging your own code is the single most important skill any coder will ever learn. See: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Jul 11 '18 at 14:12
  • I cant debugg this program if I have to Restart the device as soon as I shutdown the device the Debugging process finishes – user12346352 Jul 11 '18 at 14:16
  • check this[Background execution limits ](https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all) – prashant17 Jul 11 '18 at 14:21
  • Try jobservice using FireBase Job Dispatcher – prashant17 Jul 11 '18 at 14:23
  • You can _at least_ put some logging in your receivers and whatnot to inquire into the runtime behaviour. –  Jul 11 '18 at 14:23
  • [here] (https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all) it says that it doesn't work with Android 8.0 and higher am I Right with that – user12346352 Jul 11 '18 at 14:28
  • Check out this post. https://stackoverflow.com/a/7690600/5184092 You'll notice they have `android:permission="android.permission.RECEIVE_BOOT_COMPLETED"` in two places in their AndroidManifest file. It may lead you down the right path at least. Good luck! – luckyging3r Jul 11 '18 at 14:34

3 Answers3

2

try to use LOCKED_BOOT_COMPLETED receiver as follow:

<receiver
android:name=".BootReceiver"
android:directBootAware="true"
android:exported="false">
<intent-filter>
    <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
    <!-- For pre-N devices -->
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

don't forget permission

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

take a look here for more explanation

maheryhaja
  • 1,617
  • 11
  • 18
1

Now I fixed in on my own it was easier then i thought

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent i=new Intent(context, YourClass.class);

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        context.startForegroundService(i);
    }
    else {
        context.startService(i);
    }

}
user12346352
  • 176
  • 2
  • 20
0

I had some weird experience if I don't put full URI,

for example .BootCompletedIntentReceiver would become com.company.BootCompletedIntentRecevier.

I know it sounds dumb but I had many weird regressions when not explicitly stating things on the Manifest with modern SDKs.

Rock_Artist
  • 555
  • 8
  • 14