Skip to content

Instantly share code, notes, and snippets.

@shaon2016
Created January 16, 2019 12:03
Show Gist options
  • Save shaon2016/5c92e76254ec8d8042a3f117475ec8a6 to your computer and use it in GitHub Desktop.
Save shaon2016/5c92e76254ec8d8042a3f117475ec8a6 to your computer and use it in GitHub Desktop.
// For pre lollipop and post lollipop
import android.content.IntentFilter
import android.net.ConnectivityManager.CONNECTIVITY_ACTION
import android.util.Log
import com.firebase.jobdispatcher.JobParameters
import com.firebase.jobdispatcher.JobService
import com.osellers.fragments.HomeFragment
import com.osellers.receiver.ConnectivityReceiver
class NetworkSchedulerFirebaseJobDispatcherService : JobService(),
ConnectivityReceiver.ConnectivityReceiverListener {
override fun onNetworkConnectionChanged(isConnected: Boolean) {
HomeFragment.internetConnectionListener!!.callback(isConnected)
}
private val TAG = NetworkSchedulerFirebaseJobDispatcherService::class.java.simpleName
private var mConnectivityReceiver: ConnectivityReceiver? = null
override fun onCreate() {
super.onCreate()
Log.i(TAG, "Service created")
mConnectivityReceiver = ConnectivityReceiver(this)
}
override fun onStopJob(job: JobParameters?): Boolean {
Log.i(TAG, "onStopJob")
unregisterReceiver(mConnectivityReceiver)
return true
}
override fun onStartJob(job: JobParameters?): Boolean {
Log.i(TAG, "onStartJob$mConnectivityReceiver")
registerReceiver(mConnectivityReceiver, IntentFilter(CONNECTIVITY_ACTION))
return true
}
}
// initiate
private fun scheduleFirebaseJob() {
dispatcher = FirebaseJobDispatcher(GooglePlayDriver(context));
val myJob = dispatcher.newJobBuilder()
.setService(NetworkSchedulerFirebaseJobDispatcherService::class.java) // the JobService that will be called
.setTag(C.NETWORK_JOB_SCHEDULE_TAG) // uniquely identifies the job
.setConstraints(Constraint.ON_ANY_NETWORK, Constraint.DEVICE_CHARGING)
.build()
dispatcher.mustSchedule(myJob);
}
//Cancel the job
override fun onStop() {
dispatcher.cancel(C.NETWORK_JOB_SCHEDULE_TAG);
super.onStop()
}
// Add them on android manifest
<service
android:exported="false"
android:name=".services.NetworkSchedulerFirebaseJobDispatcherService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
// For post lollipop only
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.util.Log;
import com.osellers.fragments.HomeFragment;
import com.osellers.receiver.ConnectivityReceiver;
import androidx.annotation.RequiresApi;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class NetworkSchedulerService extends JobService implements
ConnectivityReceiver.ConnectivityReceiverListener {
private static final String TAG = NetworkSchedulerService.class.getSimpleName();
private ConnectivityReceiver mConnectivityReceiver;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service created");
mConnectivityReceiver = new ConnectivityReceiver(this);
}
/**
* When the app's NetworkConnectionActivity is created, it starts this service. This is so that the
* activity and this service can communicate back and forth. See "setUiCallback()"
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return START_NOT_STICKY;
}
@Override
public boolean onStartJob(JobParameters params) {
Log.i(TAG, "onStartJob" + mConnectivityReceiver);
registerReceiver(mConnectivityReceiver, new IntentFilter(CONNECTIVITY_ACTION));
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.i(TAG, "onStopJob");
unregisterReceiver(mConnectivityReceiver);
return true;
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
HomeFragment.internetConnectionListener.callback(isConnected);
}
}
// initiate
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private fun scheduleJob() {
val myJob = JobInfo.Builder(0, ComponentName(context!!, NetworkSchedulerService::class.java))
.setRequiresCharging(true)
.setMinimumLatency(1000)
.setOverrideDeadline(2000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.build()
val jobScheduler = context!!.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
jobScheduler!!.schedule(myJob)
}
override fun onStart() {
super.onStart()
val startServiceIntent = Intent(context, NetworkSchedulerService::class.java)
context?.startService(startServiceIntent)
}
override fun onStop() {
context?.stopService(Intent(context, NetworkSchedulerService::class.java))
super.onStop()
}
// Add them on android manifest
<service
android:name=".services.NetworkSchedulerService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment