1

I am trying to developing JobScheduler, and I want the App to do something every 1 minute when App has been killed by system.

AndroidManifest.xml

<service
    android:name=".BaiduPush.BaiduJobService"
    android:enabled="true"
    android:permission="android.permission.BIND_JOB_SERVICE"/>

MainActivity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main2)


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            startJobService()
        }
}

private fun startJobService() {
        Log.d(TAG,"startBaiduJobService")
        val JOB = 10
        val jobScheler = getSystemService((Context.JOB_SCHEDULER_SERVICE)) as JobScheduler
        val jobinfo = JobInfo.Builder(JOB, ComponentName(packageName,BaiduJobService::class.java.name))
                //.setPeriodic(AlarmManager.INTERVAL_FIFTEEN_MINUTES)
                .setOverrideDeadline(60000)
                .setPersisted(true)
                .build()

        jobScheler.schedule(jobinfo)
    }

BaiduJobService

class BaiduJobService : JobService() {

    val TAG = "BaiduJobService"

    override fun onStartJob(params: JobParameters?): Boolean {
        Log.d(TAG,"BaiduJobService onStartJob")
        return true
    }
    override fun onStopJob(params: JobParameters?): Boolean {
        return false
    }

}

When I use the setPeriodic(AlarmManager.INTERVAL_FIFTEEN_MINUTES). The onStartJob has not been called.

When I use the setOverrideDeadline(60000). The onStartJob only show once times at the first.

But the BaiduJobService seems not called when time is up.

Did I missing something? Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Wun
  • 6,211
  • 11
  • 56
  • 101

1 Answers1

0

I am Using Alarm Manager For Keep my service alive it is better than a job scheduler. I implement this code in service oncreate() method.

public class CallService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            Intent intent = new Intent(this, RestartServiceBroadcast.class);
            mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            ((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, 60000, mKeepAlivePendingIntent);
        }

    }

Create BroadcastReceiver to call service again in onRecieve

public class RestartServiceBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("service.RestartService")) {
        // Here Call a Service
    }

    }
}

Manifest Like this

 <receiver
                android:name=".service.receiver.RestartServiceBroadcast"
                android:enabled="true"
                android:exported="true"
                android:process=":sipStack">
                <intent-filter>
                    <action android:name="service.RestartService" />
                </intent-filter>
   </receiver>

        <service
            android:name=".service.CallService"
            android:enabled="true"
            android:exported="false"
            android:stopWithTask="false">

        </service>
Mayur
  • 735
  • 4
  • 14
  • Does the `AlarmManager` will launch when the App has been killed by Android system ? – Wun Jun 28 '19 at 07:02
  • Yes, I am tested in android oreo version and its working fine for me – Mayur Jun 28 '19 at 07:03
  • I have try, but it seems not alarm when the App has killed from background. Can you post your code? – Wun Jun 28 '19 at 07:35
  • yaa sure.. Happy Coding – Mayur Jun 28 '19 at 08:51
  • Is your debug android device version Android Oreo ? – Wun Jun 28 '19 at 08:56
  • Yes, I am testing in oreo with all OS like oxygen, color os, and miui os, I am developing a VoIP Application and in my application one sip service is continuing running after an app is destroying or killed. and one more thing you are set your app is always running background and battery optimization. – Mayur Jun 28 '19 at 09:00