foreground android服务有时无法使用

问题描述 投票:0回答:1

我在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>

android foreground-service
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.