如何删除未使用的GcmTaskService服务? [英] How to Remove an unused GcmTaskService service?

查看:79
本文介绍了如何删除未使用的GcmTaskService服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码GcmTaskService服务代码,可以检查互联网是否可以在线推送一些数据:

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();
    }
}
}

现在我不再在应用程序中使用它.当我删除该类时,该服务仍在后台运行,因此有时App崩溃说

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

如何完全删除该服务?

推荐答案

尝试取消与该服务相关的所有任务,然后看看会发生什么:

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

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

另一方面,OneoffTask不支持在重新启动后保持不变.

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

这篇关于如何删除未使用的GcmTaskService服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆