androidandroid-api-30

Can I start my app after booting on Android API 30?


public class Starter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BOOT_CHECK", "Receiver Arrive_1");
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.e("BOOT_CHECK", "Receiver Arrive_2");
Intent i = new Intent(context, NetworkCheckService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
try {
pendingIntent.send();
Log.e("BOOT_CHECK", "pending : "+pendingIntent.toString());
Log.e("BOOT_CHECK", "Receiver Arrive_3");
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}

        }
        Log.e("BOOT_CHECK", "Receiver Arrive_4");
    }

}
**Manifest.xml**
\<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /\>
\<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /\>
...
\<receiver android:name=".Starter"
android:enabled="true"
android:exported="false"
android:label="StartReceiver"\>
\<intent-filter\>
\<action android:name="android.intent.action.BOOT_COMPLETED" /\>
\<category android:name="android.intent.category.DEFAULT" /\>
\</intent-filter\>
\</receiver\>

** When booting my app I Can Check my logs which in logcat, but it doesn't works ** and also when I changed the code like "startForegroundService" doesn't works


Solution

  • Your problem is that a BroadcastReceiver can't start a foreground service, and a background service will be killed in 2 minutes. So what you're trying to do can't be done. However, I question if you need to- your service is called network check service. If all you want to do is check if the network is up to do some work, JobScheduler is a better fit.