Search code examples
androidforeground-service

foreground android service not works sometime's


i have a foreground service in android that execute some task every minutes. but after logging it i find out it won't fire or something else every minute. what can id do to be sure it fire. i should mention that i use react native and my task is in react native side with headlessjs task, but i think that's not the problem. i start service inside of my code and it works in debuggign but after release it not fire every minutes and fire in random interval! please help me?


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>


Solution

  • it seem's i just needed to add android:process=":custom_text" on my service tag in my manifest in order to separate thread of service and my app, and now it work's. i hope this help some one.