Я запускаю функцию в фоновом режиме, но я хотел бы продолжить ее, когда выхожу на передний план без перезапуска. Это возможно в первую очередь? Я делаю это в RN (android).
Ниже я публикую код из кода BackgroundService Редактировать:
public class BackgroundService extends Service {
private static final int SERVICE_NOTIFICATION_ID = 12345;
private static final String CHANNEL_ID = "headless_task";
private Handler handler = new Handler();
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
Context context = getApplicationContext();
Intent myIntent = new Intent(context, BackgroundEventService.class);
context.startService(myIntent);
//HeadlessJsTaskService.acquireWakeLockNow(context);
//handler.postDelayed(runnableCode, 2000);
}
};
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "headless_task", importance);
channel.setDescription("CHANEL DESCRIPTION");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
this.handler.removeCallbacks(this.runnableCode);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.handler.post(this.runnableCode);
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("myapp").setContentText("Running...").setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent).setOngoing(true).build();
startForeground(SERVICE_NOTIFICATION_ID, notification);
return START_STICKY;
}
}