< /p>
Код: Выделить всё
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://example.com");
setContentView(webView);
if (getIntent().getBooleanExtra("launchedFromBoot", false)) {
Toast.makeText(this, "Auto-launched after reboot", Toast.LENGTH_LONG).show();
}
}
< /code>
service < /p>
public class BootService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotificationChannel();
Notification notification = new NotificationCompat.Builder(this, "boot_channel")
.setContentTitle("Auto Start")
.setContentText("BootService running...")
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.build();
startForeground(1, notification);
new Handler(Looper.getMainLooper()).postDelayed(() -> {
Intent launchIntent = new Intent(this, MainActivity.class);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntent.putExtra("launchedFromBoot", true);
startActivity(launchIntent);
}, 3000);
return START_NOT_STICKY;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"boot_channel", "Boot Service Channel", NotificationManager.IMPORTANCE_HIGH);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
}
@Override
public IBinder onBind(Intent intent) { return null; }
}
< /code>
приемник < /p>
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BootReceiver", "Boot completed - starting service");
Intent serviceIntent = new Intent(context, BootService.class);
context.startForegroundService(serviceIntent); // Android 8+
}
}
< /code>
manifest < /p>
Подробнее здесь: https://stackoverflow.com/questions/796 ... iver-foreg
Мобильная версия