前台 android 服务有时无法正常工作

foreground android service not works sometime's

我在 android 有一个前台服务,每分钟执行一些任务。但是在记录它之后我发现它不会每分钟都触发或发生其他事情。我能做些什么来确保它开火。我应该提一下,我使用的是 React Native,我的任务是在 React Native 端使用 headlessjs 任务,但我认为这不是问题所在。我在我的代码中开始服务,它在调试中工作,但在发布后它不会每分钟触发一次,而是随机触发一次!请帮帮我?


public class TimeCheckerService extends Service {

    private static final int SERVICE_NOTIFICATION_ID = 12345;
    private static final String CHANNEL_ID = "TimeChecker";

    private Handler handler = new Handler();
    private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {
            Context context = getApplicationContext();
            Intent myIntent = new Intent(context, TimeCheckerEventService.class);
            context.startService(myIntent);
            HeadlessJsTaskService.acquireWakeLockNow(context);
            handler.postDelayed(this, 60000);
        }
    };

    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_MAX;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "TimeChecker", importance);
            channel.setDescription("ُسلاما");
            channel.setSound(null, null);
            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);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("سلاما").setContentText("با سلاما، سلامت بمانید").setSmallIcon(R.drawable.ic_notif)
                .setLargeIcon(
                        BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
                .setContentIntent(contentIntent).setOngoing(true).setSound(null);
        startForeground(SERVICE_NOTIFICATION_ID, notification.build());
        return START_STICKY;
    }
}

public class TimeCheckerEventService extends HeadlessJsTaskService {
    @Nullable
    protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
        Bundle extras = intent.getExtras();
        //HeadlessJsRetryPolicy retryPolicy = new LinearCountingRetryPolicy(3,1000);
        return new HeadlessJsTaskConfig(
                "TimeChecker",
                extras != null ? Arguments.fromBundle(extras) : null,
                5000,
                true);
    }
}
            <service
                android:name="com.salamaa.TimeCheckerService"
                android:enabled="true"
                android:exported="false" >
            </service>
            <service android:showOnLockScreen="true" android:name="com.salamaa.TimeCheckerEventService"></service>
            <receiver
                android:name="com.salamaa.BootUpReceiver"
                android:enabled="true"
                android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </receiver>

看来我只需要在我的清单中的服务标签上添加 android:process=":custom_text" 以便将服务线程和我的应用程序分开,现在它可以工作了。我希望这对某些人有所帮助。