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

时间:2019-09-27 19:18:50

标签: android foreground-service

我在android中有一个前台服务,每分钟执行一些任务。但是登录后,我发现它不会每分钟触发或发生其他事件。 id可以做什么以确保其触发。我应该提一下,我使用react native,而我的任务是与headlessjs任务在React native方面,但我认为这不是问题。我在我的代码中启动服务,它可以在debuggign中工作,但发布后不会每分钟触发一次,并且会随机触发!请帮帮我吗?


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>

1 个答案:

答案 0 :(得分:0)

似乎我只需要在清单中的服务标签上添加android:process =“:custom_text”即可分隔服务线程和应用程序,现在它就可以了。希望对您有所帮助。