gpt4 book ai didi

java - Android 地理位置(当应用程序未运行时)

转载 作者:行者123 更新时间:2023-12-01 04:26:04 26 4
gpt4 key购买 nike

我正在寻找有关全天在 Android 上检查位置(大约每 15 分钟)并存储地理坐标的信息。当应用程序在后台时也会发生这种情况,而不仅仅是当应用程序在前台运行时。我不确定搜索词,我似乎找不到任何东西。任何人都可以建议搜索词来为我指明正确的方向吗?

非常感谢,凯文

编辑:

public class MyService extends Service
{
private static final String TAG = "Location";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;

private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider)
{
Log.i(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
public void onLocationChanged(Location location)
{
Log.i(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}
public void onProviderDisabled(String provider)
{
Log.i(TAG, "onProviderDisabled: " + provider);
}

public void onProviderEnabled(String provider)
{
Log.i(TAG, "onProviderEnabled: " + provider);
}

public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.i(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate()
{
Log.i(TAG, "onCreate");
initializeLocationManager();
try {
Thread LocThread = new Thread(new Runnable() {
public void run() {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
}
});

try {
LocThread.join();
} catch (InterruptedException e) {
Log.i("LocationError","Thread could not join");
e.printStackTrace();
}

} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.i(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
Thread LocThread = new Thread(new Runnable() {
public void run() {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
});

try {
LocThread.join();
} catch (InterruptedException e) {
Log.i("LocationError","Thread could not join");
e.printStackTrace();
}

}
}
}

最佳答案

您应该阅读有关如何获取 Location Information 的信息在 android 和 How to schedule a service periodically using AlarmManager.

关于java - Android 地理位置(当应用程序未运行时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482273/

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