服务冻结活动?

发布于 2024-12-09 22:42:16 字数 97 浏览 3 评论 0原文

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

I have a service that gets the location and sends it to a server, also I have an activity that starts and stops the service with a button. When I start up the service all the buttons do not work and after a while the activity force closes giving the option to wait or close. what could be causing the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-12-16 22:42:16

我以前做过类似的事情......我不发布整个代码,只是主要的事情。这不会运行,我只是想给你一些想法,告诉你该做什么以及如何处理它。
魔法是在 sendUpdatesToUI 中完成的...我自己保存了所有不重要的方法,这些方法在您实现 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;

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

一个想法,如何编程一个位置线程,这不会冻结你的 UI。

玩得开心

I did something like that before ... I dont post the whole code, jsut the main thing. This wont run, i just want to give you the idea, of what to llok for and how to handle it.
The magic is done in sendUpdatesToUI... I saved myself all the unimportant methods that are overwritten when you implement LocationListener - you know best, which of them you need.

class:

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;

}

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

It probably won't run that way, because I kicked out all the uninteresting stuff, but it should give you an idea, how to programm a locationthread, which won't freeze your UI.

Have fun

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文