问题描述
我想开发基于位置的提醒应用程序。因此,我想使用Android的服务,为报复应用程序没有运行的当前位置。但我没有这样做。我设置计时器在Android的服务类,但我不知道如何在服务类中的当前位置。有什么问题 ?我得到了一些错误是这样的:
不能创建内螺纹的处理程序已经不叫尺蠖。prepare()
公共类TrackerService延伸服务{ 双latShared;
双lngShared;
双latService;
双lngService;
私人的LocationManager流明;
定时器定时器;
处理程序处理程序;
最终静态时间长= 15000;
共享preferences mShared preFS;
共享preferences.Editor米prefsEditor; @覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
返回null;
} @覆盖
公共无效的onCreate(){
mShared preFS = getShared preferences(locationXML,MODE_PRIVATE);
latShared =(双)mShared prefs.getFloat(纬度,0);
lngShared =(双)mShared prefs.getFloat(LNG,0);
定时器=新定时器();
timer.schedule(新的TimerTask(){
@覆盖
公共无效的run(){
LocationUpdates();
}
} 0,时间); } 公共无效LocationUpdates(){
locListener locList =新locListener();
LM =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locList);
} @覆盖
公共无效的onDestroy(){
//lm.removeUpdates(this);
timer.cancel();
} 公共类locListener实现LocationListener的{ @覆盖
公共无效onLocationChanged(地点){
latService = location.getLatitude();
lngService = location.getLongitude();
} @覆盖
公共无效onProviderDisabled(字符串提供商){} @覆盖
公共无效onProviderEnabled(字符串提供商){} @覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){}
}}
LocationManager.requestLocationUpdates
应该作为 UI线程运行其兄弟方法定义。在另一方面的TimerTask
引入了一个新的线程来卸载它的执行。
不过,如果你不使用的新工艺,为您服务,然后只需拨打 LocationUpdates()
如下:
@覆盖
保护无效的onCreate(){
mShared preFS = getShared preferences(locationXML,MODE_PRIVATE);
latShared =(双)mShared prefs.getFloat(纬度,0);
lngShared =(双)mShared prefs.getFloat(LNG,0); 最后的处理程序H =新的处理程序();
h.post(新的Runnable(){
@覆盖
公共无效的run(){
LocationUpdates();
h.postDelayed(这一点,TIME);
}
});
}
或,如果你不希望使用的处理程序然后简单地升级你的 requestLocationUpdates
如下:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locList,getMainLooper());
在上述情况下, getMainLooper()
将派遣从UI线程更新自己的位置。
I want to develop location based reminder app. Therefore I want to use android service for get current location even app is not running. But I didn't do. I set timer in android service class but I don't know how to get current location in service class. What is the problem ? I got some error like this:
can't create handler inside thread that has not called looper.prepare()
public class TrackerService extends Service {
double latShared;
double lngShared;
double latService;
double lngService;
private LocationManager lm;
Timer timer;
Handler handler;
final static long TIME = 15000;
SharedPreferences mSharedPrefs;
SharedPreferences.Editor mPrefsEditor;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
mSharedPrefs = getSharedPreferences("locationXML", MODE_PRIVATE);
latShared = (double)mSharedPrefs.getFloat("lat", 0);
lngShared = (double)mSharedPrefs.getFloat("lng", 0);
timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run(){
LocationUpdates();
}
},0,TIME);
}
public void LocationUpdates(){
locListener locList = new locListener();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList);
}
@Override
public void onDestroy() {
//lm.removeUpdates(this);
timer.cancel();
}
public class locListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
latService = location.getLatitude();
lngService = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}
LocationManager.requestLocationUpdates
is supposed to run on UI thread as its sibling method defines. On the other hand TimerTask
introduces a new thread to offload its execution.
However, if you are not using a new process for your service then simply call LocationUpdates()
as below:
@Override
protected void onCreate() {
mSharedPrefs = getSharedPreferences("locationXML", MODE_PRIVATE);
latShared = (double)mSharedPrefs.getFloat("lat", 0);
lngShared = (double)mSharedPrefs.getFloat("lng", 0);
final Handler h = new Handler();
h.post(new Runnable() {
@Override
public void run() {
LocationUpdates();
h.postDelayed(this, TIME);
}
});
}
OR, if you don't want to use handler then simply upgrade your requestLocationUpdates
as below:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList, getMainLooper());
In above case, getMainLooper()
will dispatch your location updates from UI thread.
这篇关于获取当前位置从Android的后台服务问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!