如何连续在后台和前台运行任务?

时间:2020-01-27 13:06:39

标签: android react-native background-process

我正在后台运行一个函数,但是当我到达前台而无需重新启动时,我想继续运行它。首先有可能吗? 我正在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;
}

}

1 个答案:

答案 0 :(得分:0)

活动:

null

服务等级

 public class MyActivity extends Activity {

    private Intent serviceIntent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Start Foreground Service
        serviceIntent = new Intent(this, MyService.class);
        serviceIntent.putExtra("name", "ahmet vefa saruhan");
        startService(serviceIntent);
        findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Stop Foreground Service
                stopService(new Intent(getApplicationContext(),MyService.class));
            }
        });
    }
}