androidandroid-servicegcmtaskservice

How to Remove an unused GcmTaskService service?


I have this code GcmTaskService service code that check internet is ok to push some data online :

public class NetworkChangeService extends GcmTaskService {

public static String TAG = "NetworkChangeService";
private static long ONE_HOUR_IN_SECONDS = 3600L;
private static long ONE_MINUTE_IN_SECONDS = 60L;

private Synchronizer synchronizer;

public NetworkChangeService() {

}

@Override
public int onRunTask(TaskParams taskParams) {

    // do my task here like syncing stuff
    Log.d(TAG, "NetworkChangeService says that Network is Connected...");

    saveData(getApplicationContext());

    // TODO : Reschedule task in after certain condition (if failure) not to create duplicates
    //scheduleTask(NetworkChangeService.this);

    return GcmNetworkManager.RESULT_SUCCESS;
}

/**
 * Schedule a Task that will be executed on Network Connected to Upload offline Members and Ventes
 * @param context Context of the Activity or Application
 */
public void scheduleTask(Context context) {

    OneoffTask task = new OneoffTask.Builder()
            .setService(NetworkChangeService.class)
            .setTag(TAG)
            .setExecutionWindow(1L, ONE_MINUTE_IN_SECONDS)
            .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
            .setPersisted(true)
            .setUpdateCurrent(true)
            .build();

    GcmNetworkManager.getInstance(context).schedule(task);
}

@Override
public void onInitializeTasks() {
    super.onInitializeTasks();
    // Re-schedule periodic task on app upgrade.
    scheduleTask(this);
}

public void saveData(Context context) {
    // OFFLINE MODE & ONLINE MODE
    // Check if there is connection to save data offline
    // Check if forceOffline is true then save Offline
    int connectionType = NetworkUtil.getConnectivityStatus(context);

    // 0 : No Connection, 1 : Mobile, 2 Wifi
    if(connectionType > 0) {

        if(synchronizer == null)
            synchronizer = new Synchronizer();

        // Save offline unsaved Members and Ventes
        synchronizer.saveMembersOnline();
        synchronizer.saveVentesOnline();
    }
}
}

And now I'm not using it anymore in the Application. When I remove that class, the service is still running in background so that sometimes the App crashes saying

java.lang.RuntimeException: Unable to instantiate service com.ezcoding.sxc.utils.NetworkChangeService

How can I remove completely the service?


Solution

  • Try cancelling all tasks related to that service and see what happens:

    GcmNetworkManager.getInstance(this)
        .cancelAllTasks(NetworkChangeService.class)
    

    On the other hand, OneoffTask do not support to be persisted across reboots.