Restart service on device reboot

user12346352 :

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 :

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

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104481&siteId=1