Android Can';不要停止服务

Android Can';不要停止服务,android,service,android-service,Android,Service,Android Service,我正在开发一个应用程序,通过POST请求启动从服务器获取信息的服务。 在我的例子中,用户可以取消加载数据(当他在ListViewstartednewactivity中选择item时,我在线程中启动服务) 现在,如果用户按Back返回到previos活动,我调用了stopService(intent)。调用了方法onDestroy(),但从Internet加载数据不会停止 我的服务起点: private void makeNewSync() { Thread thread = ne

我正在开发一个应用程序,通过POST请求启动从服务器获取信息的服务。 在我的例子中,用户可以取消加载数据(当他在
ListView
startednewactivity中选择item时,我在线程中启动服务)

现在,如果用户按Back返回到previos活动,我调用了
stopService(intent)
。调用了方法
onDestroy()
,但从Internet加载数据不会停止

我的服务起点:

private void makeNewSync() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Intent intent =new Intent(getApplicationContext(),NewsLoaderService.class);
                intent.putExtra("NEW_GLOBAL_ID", newGlobalId);
                startService(intent);
            }
        });
        thread.setPriority(10);
        thread.start();
    }
我的后退按钮侦听器:

     @Override
            public void onBackPressed() {
                if (isFullScreen) {
                    changeFullScreenMode(View.GONE);
                } else {
                    stopService(new Intent(getApplicationContext(),NewsLoaderService.class));
                    super.onBackPressed();
                }
            }
我的服务的onDestroy()方法:

@Override
    public void onDestroy() {
        Log.d("MyLOg", "loader service is: " + isRunning + " stopped.");
        if (isRunning) {
            isRunning = false;
        }
        super.onDestroy();
    }

当您使用stopService方法停止服务时,如果您的服务当前正在运行,这将调用服务的onDestroy方法。在onDestroy方法中,您编写代码来停止任务。

不要在单独的线程中启动服务,也不要在您的情况下使用IntentService,或者通过处理程序启动服务,因为服务在ui线程中运行

哪种代码可以终止我的服务(带有服务的任务)?例如,如果服务方法onStartCommand启动Thread,那么您可以调用Thread.stop()方法。这只是一个如何停止服务的后台任务的示例。您可以在服务的onDestory方法中使用flag变量并使其为false。首先解除服务绑定。如果他使用INTERNET,在UI线程中启动服务?名为android.os.NetworkOnMainThreadException的系统。你的问题不是个好主意