gpt4 book ai didi

java - Android - 位置管理器 requestLocationUpdates 瓶颈

转载 作者:行者123 更新时间:2023-11-30 03:24:26 24 4
gpt4 key购买 nike

我有一个 Android 后台服务,它会不时报告位置。当我通过 wifi 在本地测试时它工作得很好,但是当在 3G 连接中测试时(有时在 Edge 上)我发现应用程序显然进入了瓶颈并且不执行 onLocationChanged 方法。没关系,因为可能会丢失信号等等。然而,过了一会儿(也许当连接重新建立时)它开始立即更新所有请求,在几秒钟内多次执行方法 onLocationChanged .

有没有人知道如何解决这个问题?是否可以将超时添加到方法 locationManager.requestLocationUpdates 中?

我的听众

public class MyListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
//report location to server
HttlCallToUpdatePostion(loc.Latitude, loc.Longitude, loc.Accuracy);
}
}

我的服务

Handler handler = null;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyListener listener = new MyListener();

protected void doWork() {
Looper.prepare();
handler = new Handler();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, listener);
Looper.loop();
}

最佳答案

我写了一个应用程序,正是您所需要的。当它只是一项服务时,我遇到了同样的问题。当 UI 进入后台并关闭屏幕时,服务进入后台并安排系统调用,一旦触发,缓冲区就会被刷新,我有大约 10-50 次更新。

解决方案是:必须使用 5000 值设置和安排警报,BroadcastRreceiver 将接收并正确处理。比你会遇到其他问题,这里没有问。

对我来说,这是一个解决方案,应用程序正在使用中!

编辑:报警设置代码部分:

Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
// In reality, you would want to have a static variable for the request
// code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), timerInterval, sender);

安卓 list 文件:

<receiver  android:process=":remote" android:name=".broadcastreceiver.AlarmReceiver"/>

类实现部分:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Context appContext = context.getApplicationContext();
...

关于java - Android - 位置管理器 requestLocationUpdates 瓶颈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com