Android 服务冻结活动?

Android 服务冻结活动?,android,service,android-activity,freeze,Android,Service,Android Activity,Freeze,我有一个获取位置并将其发送到服务器的服务,还有一个通过按钮启动和停止服务的活动。当我启动服务时,所有按钮都不工作,一段时间后,活动强制关闭,提供等待或关闭选项。是什么导致了这个问题?我以前做过类似的事情。。。我不会发布全部代码,而是主要内容。这不会运行,我只是想给你一个想法,什么样的玩乐,以及如何处理它。 魔法是在SendUpdateStoreUI中完成的。。。我保存了所有在实现LocationListener时被覆盖的不重要的方法—您最清楚,您需要哪种方法 类别: public class S

我有一个获取位置并将其发送到服务器的服务,还有一个通过按钮启动和停止服务的活动。当我启动服务时,所有按钮都不工作,一段时间后,活动强制关闭,提供等待或关闭选项。是什么导致了这个问题?

我以前做过类似的事情。。。我不会发布全部代码,而是主要内容。这不会运行,我只是想给你一个想法,什么样的玩乐,以及如何处理它。 魔法是在SendUpdateStoreUI中完成的。。。我保存了所有在实现LocationListener时被覆盖的不重要的方法—您最清楚,您需要哪种方法

类别:

public class ServiceLocator extends Service implements LocationListener 
public static final String BROADCAST_ACTION = "my.app.isgreat";
private final Handler handler = new Handler();
private LocationManager locationManager;
Intent intent;

public void onCreate() {
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    intent = new Intent(BROADCAST_ACTION);
}

public void onStart(Intent intent, int startId) {
    handler.removeCallbacks(sendUpdatesToUI);
    handler.postDelayed(sendUpdatesToUI, DEBUG_DELAY);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            DEBUG_DELAY, 3, this);

}

@Override
public void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(sendUpdatesToUI);
    locationManager.removeUpdates(this);
    locationManager = null;
}

它可能不会以这种方式运行,因为我排除了所有无趣的东西,但它应该给你一个想法,如何编程locationthread,它不会冻结你的UI

玩得开心

//Here is the second thread, that won'z freeze your UI
//DisplayLogginInfo() sets all the values you wanna send
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
    DisplayLoggingInfo();
    handler.postDelayed(this, DEBUG_DELAY);
}

};

private void DisplayLoggingInfo() {
    intent.putExtra("long", String.format("%.4f", lastLocation.getLongitude()));
    sendBroadcast(intent);
}