如何启用Android后台位置更新?
嘿大家, 我正在编写一个应用程序,当用户从 A 点步行到 B 点时,该应用程序使用地理定位来跟踪用户。这是到目前为止我的代码:
public class LocationTest extends Activity {
private static final String[] S = { "out of service", "temporarily unavailable", "available" };
ArrayList<Location> list = new ArrayList<Location>();
private TextView output;
private String best;
LocationListener locationListener;
LocationManager mgr;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView) findViewById(R.id.output);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
log("\nBest provider is: " + best);
locationListener = new LocationListener(){
public void onLocationChanged(Location location){
dumpLocation(location);
list.add(location);
}
public void onStatusChanged(String provider, int status, Bundle extras){
log("\nProvider status changed: " + provider + ", status=" + S[status]);
}
public void onProviderEnabled(String provider){
log("\nProvider enabled: " + provider);
}
public void onProviderDisabled(String provider){
log("\nProvider disabled: " + provider);
}
};
}
@Override
protected void onResume(){
super.onResume();
mgr.requestLocationUpdates(best, 120000, 50, locationListener);
}
@Override
protected void onPause(){
super.onPause();
mgr.removeUpdates(locationListener);
log_gen(list);
}
每当获得新的修复时,该应用程序当前都会显示经度和纬度。但是,只有当 Activity 显示在屏幕上时,跟踪才会起作用,并且一旦用户退出应用程序,跟踪就会停止。我希望我的应用程序做的是在后台继续跟踪用户,即使他退出应用程序也是如此。例如,每当他在几分钟后重新打开应用程序时,在后台捕获的所有坐标都应显示在屏幕上。
根据我到目前为止的研究,有两种方法可以实现这一点:要么使用后台服务进行跟踪,要么
requestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent)
与 BroadcastReceiver
结合使用,即使用户退出,也可以继续获取位置更新。应用程序。如果我理解正确,第二种方法将继续在后台运行。有人可以在代码中向我展示如何使用 requestLocationUpdates
的替代版本实现 BroadcastReceiver
提前非常感谢。
Hey everybody,
I am writing an app that uses geolocation to track a user while he is walking from point A to point B. Here is my code so far:
public class LocationTest extends Activity {
private static final String[] S = { "out of service", "temporarily unavailable", "available" };
ArrayList<Location> list = new ArrayList<Location>();
private TextView output;
private String best;
LocationListener locationListener;
LocationManager mgr;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView) findViewById(R.id.output);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
log("\nBest provider is: " + best);
locationListener = new LocationListener(){
public void onLocationChanged(Location location){
dumpLocation(location);
list.add(location);
}
public void onStatusChanged(String provider, int status, Bundle extras){
log("\nProvider status changed: " + provider + ", status=" + S[status]);
}
public void onProviderEnabled(String provider){
log("\nProvider enabled: " + provider);
}
public void onProviderDisabled(String provider){
log("\nProvider disabled: " + provider);
}
};
}
@Override
protected void onResume(){
super.onResume();
mgr.requestLocationUpdates(best, 120000, 50, locationListener);
}
@Override
protected void onPause(){
super.onPause();
mgr.removeUpdates(locationListener);
log_gen(list);
}
The app currently displays longitude and latitude whenever new fix is obtained. However, the tracking only works when the Activity is displayed on the screen and as soon as the user quits the app the tracking stops. What I want my app to do is keep tracking the user in the background even if he quits the app. Whenever he re-opens an app few minutes later, for example, all the coordinates captured in the background should be displayed on the screen.
From what I researched so far, there are two ways one can go about it: either use a background service to do the tracking or use
requestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent)
in combination with BroadcastReceiver
to continue getting location updates even if the user quits the app. If I am understanding correct, the second method would continue running in the background. Can someone please show to me in code how to implement BroadcastReceiver
with the alternate version of requestLocationUpdates
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用下面的代码,您可以很好地获得定期位置更新,如果担心连续运行的服务,您可以自定义获取位置更新的时间间隔,您还需要在应用程序中集成 google play 服务,以便以下代码正常工作
有关集成 Google Play 服务的信息,请参阅此处
Using the below code you can get the periodic location updates well if are concerned for the continuously running service you can customise the interval for getting the location updates also you would need to integrate the google play services in you app in order the following code to work
For integrating google play services please refer here