У меня есть сервис переднего плана в Android, который выполняет какую-то задачу каждую минуту.но после регистрации я обнаружил, что он не будет срабатывать или что-то еще каждую минуту.что я могу сделать, чтобы убедиться, что это огонь.Я должен упомянуть, что я использую реагировать нативно, и моя задача состоит в том, чтобы реагировать нативно с задачей 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>