如何保持一个IntentService的主题还活着吗?主题、IntentService

2023-09-04 13:14:35 作者:本性狂野

我在写一个测试应用程序,它会跟踪用户的位置。我们的想法是,我可以启动一个服务,然后注册了位置更新。现在我使用的是IntentService为。

服务code(这是不工作...)看起来就像是:

 公共类GpsGatheringService扩展IntentService {

//瓦尔
私有静态最后字符串变量=GpsGatheringService;
私人布尔stopThread;

//构造
公共GpsGatheringService(){
    超级(GpsGatheringServiceThread);
    stopThread = FALSE;
}

//方法
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
    Log.d(TAG,onStartCommand());
    返回super.onStartCommand(意向,标志,startId);
}

@覆盖
保护无效onHandleIntent(意向为arg0){
    //这是一个专门的线程中运行
    Log.d(TAG,onHandleIntent());
    LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
    LocationListener的LocationListener的=新LocationListener的(){

        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
            Log.d(TAG,onStatusChanged());
        }

        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            Log.d(TAG,onProviderEnabled());
        }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            Log.d(TAG,onProviderDisabled());
        }

        @覆盖
        公共无效onLocationChanged(位置定位){
            Log.d(TAG,onLocationChanged());
        }
    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);

    而(!stopThread){
        尝试 {
            Log.d(TAG,要睡觉......);
            视频下载(1500);
        }赶上(InterruptedException异常E){
            e.printStackTrace();
        }
    }
}

@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    Log.d(TAG的onDestroy());
    stopThread = TRUE;
}
 

}

目前其发生的唯一一件事就是去睡觉......的输出。我需要一些机制这使线程活着(因为其他的侦听器,且不能再进行状态更新),并且不浪费CPU时间(我想忙循环不是preferred方式)。 即使有吨的其他方式如何实现应用程序的行为(全球定位系统记录坐标)我感兴趣的一个解决方案,以这种方式来学习一门技术来解决这种问题!

解决方案   

如何保持一个IntentService的主题还活着吗?

手写文字图片 原来,我一直都只是一个人

您不知道。不要使用 IntentService 中的场景是这样的。

  

我需要一些机制这使线程活着(因为其他的侦听器,且不能再进行状态更新),并且不浪费CPU时间(我想忙循环不是preferred方式)

没有,你不知道。你需要学习如何解决的正确方法问题。在这种情况下,服务是好的,因为您可以控制​​在服务要走了,你控制你创建的线程等的寿命 IntentService 是不适合你的预期目的,当服务是怎么回事了,因为的它的控制和是的控制线程的生命周期。

I'm writing a test application which keeps track of the users location. The idea is that i can start a service which then registers for location updates. For now I'm using an IntentService for that.

The service code (which is not working...) looks like that:

public class GpsGatheringService extends IntentService {

// vars
private static final String TAG = "GpsGatheringService";
private boolean stopThread;

// constructors
public GpsGatheringService() {
    super("GpsGatheringServiceThread");
    stopThread = false;
}

// methods
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand()");
    return super.onStartCommand(intent, flags, startId);
}

@Override
protected void onHandleIntent(Intent arg0) {
    // this is running in a dedicated thread
    Log.d(TAG, "onHandleIntent()");
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "onStatusChanged()");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, "onProviderEnabled()");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG, "onProviderDisabled()");
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, "onLocationChanged()");
        }
    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    while (!stopThread) {
        try {
            Log.d(TAG, "Going to sleep...");
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy()");
    stopThread = true;
}

}

Currently the only thing which happens is the output of "Going to sleep...". I need some mechanism which keeps the thread alive (because else the listener is not reachable anymore for status updates) and doesnt waste cpu time (I think busy looping is not the preferred way). Even if there are tons of other ways how to realize the application behaviour (logging of gps coordinates) I am interested in a solution to this way to learn a technique to solve problems of this nature!

解决方案

How do I keep the Thread of an IntentService alive?

You don't. You do not use IntentService in a scenario like this.

I need some mechanism which keeps the thread alive (because else the listener is not reachable anymore for status updates) and doesnt waste cpu time (I think busy looping is not the preferred way)

No, you do not. You need to learn how to solve problems in the proper way. In this case, a Service is fine, because you control when the service is going away, you control the lifetime of any threads you create, etc. IntentService is unsuitable for your intended purpose, because it controls when the service is going away and it controls the lifetime of the threads.