0
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

ravaenee
  • 1
  • 3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 25 '23 at 10:05

1 Answers1

0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Oh thanks for Answer~ Ah it's just try for working test because PendingIntent.getActivity has same reaction so your Idea need some delay before activce? – ravaenee Jul 25 '23 at 05:31
  • That won't work either- a broadcast receiver can't launch anything in the foreground except under special circumstances: https://developer.android.com/guide/components/activities/background-starts – Gabe Sechan Jul 25 '23 at 05:32
  • I see I see, oh it' works when I changed getActivity and it's follow IntroActivity.class but it's need to run once just first time – ravaenee Jul 25 '23 at 05:41