Android 位置服务的更新间隔
我正在编写一个位置服务,以更新间隔将位置更新发送到服务器。 但是我试图通过用户输入(Android 位置服务的更新间隔,android,service,locationlistener,Android,Service,Locationlistener,我正在编写一个位置服务,以更新间隔将位置更新发送到服务器。 但是我试图通过用户输入(AlertDialog)在服务中更新此间隔变量。它在硬编码时工作得非常好 我使用此代码从服务的onCreate()中的AlertDialog类中获取间隔变量 public void onCreate() { super.onCreate(); final boolean tr=true; new Thread(new Runnable() { publi
AlertDialog
)在服务中更新此间隔变量。它在硬编码时工作得非常好
我使用此代码从服务的onCreate()
中的AlertDialog
类中获取间隔变量
public void onCreate() {
super.onCreate();
final boolean tr=true;
new Thread(new Runnable() {
public void run() {
while (tr) {
//check your static variable here
updateInterval=ShowCurInterval.loadCurInterval(getApplicationContext());//ShowCur Interval is the Alert Dialog calss
Log.d(" INTERVAL ","Interval "+ updateInterval);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}
).start();
startLocationListener(updateInterval);
}
我还可以在日志中看到新的updateInterval值(从警报对话框添加)。但是requestLocationUpdates()
仍然使用预定义的updateInterval值
下面是startLocationListener()方法:
是否有人对如何更新此变量有任何建议
@平房
已编辑部分,但有例外:
Handler mhandler = new Handler ();
mhandler.postDelayed( new Runnable(){
public void run(){
Looper.prepare();
updateInterval=SingletonManager.getInstance().loadCurInterval(getApplicationContext());
SingletonManager.getInstance().saveCurInterval(getApplicationContext(), updateInterval);
startLocationListener(updateInterval);
Log.d(" INTERVAL ","Interval "+ updateInterval);
//startChecking();
}
}, 2000);
例外情况:
04-17 03:18:55.250: E/AndroidRuntime(2146): java.lang.RuntimeException: Only one Looper may be created per thread
提前感谢。您正在onCreate()中调用startLocationListener(),而不是在为获取updateInterval的新值而创建的线程中。但是调用startLocationListener(updateInterval);在新线程执行之前执行,因此您将获得updateInterval的旧值。我认为您应该将代码更改为以下内容:
public Handler handler;
public void onCreate() {
super.onCreate();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
startLocationListener(updateInterval);
}
};
final boolean tr=true;
new Thread(new Runnable() {
public void run() {
while (tr) {
//check your static variable here
updateInterval=ShowCurInterval.loadCurInterval(getApplicationContext());//ShowCur Interval is the Alert Dialog calss
handler.sendEmptyMessage(1);
Log.d(" INTERVAL ","Interval "+ updateInterval);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}
).start();
}
发生了什么事?它仅适用于以前的更新速率吗?@raju Hello..是的,它仅适用于以前的更新速率。updateInterval是一个值为10000(毫秒)的全局变量。04-16 08:15:43.850:E/AndroidRuntime(13728):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序,并且startLocationListener()未启动。startLocationListener()仅当它位于线程外部时启动。请告诉我应该做什么。使用处理程序在主活动线程中启动位置侦听器,而不是您正在创建的新线程。我对答案进行了修改,以便做到这一点。另外,我必须提到,我不喜欢在这种情况下使用线程。这一切只能通过处理程序完成。谢谢你的回答…现在我可以启动位置侦听器了..但是updateInterval值仍然没有更新。你能告诉我哪里出了问题吗。
public Handler handler;
public void onCreate() {
super.onCreate();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
startLocationListener(updateInterval);
}
};
final boolean tr=true;
new Thread(new Runnable() {
public void run() {
while (tr) {
//check your static variable here
updateInterval=ShowCurInterval.loadCurInterval(getApplicationContext());//ShowCur Interval is the Alert Dialog calss
handler.sendEmptyMessage(1);
Log.d(" INTERVAL ","Interval "+ updateInterval);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}
).start();
}