gpt4 book ai didi

android - 在特定时间间隔后获取当前设备位置

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:35 24 4
gpt4 key购买 nike

我想在特定时间间隔(比如 30 分钟)后捕获 Android 设备的当前位置(纬度和经度)。我的类(class)(或服务 ?? 不确定要使用什么)将开始收听 LocationManagerListener 当设备启动完成时。实现这个的最佳方法是什么?在这种情况下我们如何使用 locationChanged() 方法?

这是我认为它可以做到的:

监听开机完成事件并设置告警服务:

public class OnBootReceiver extends BroadcastReceiver {
private static final int PERIOD=1800000; // 30 minutes

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

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0,
i, 0);

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+60000,
PERIOD,
pi);
}
}

监听警报服务并启动位置捕获类或服务:

 public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakefulIntentService.acquireStaticLock(context);
context.startService(new Intent(context, locationCapture.class));
or
new locationCapture().classmethod();
}
}

我不确定 locationCapture 类应该如何实现。应该是普通的Java类还是Service类?

我们将不胜感激。

最佳答案

这是您可以使用的服务类

public class ServiceLocation extends Service{
private LocationManager locMan;
private Boolean locationChanged;
private Handler handler = new Handler();

public static Location curLocation;
public static boolean isService = true;

LocationListener gpsListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (curLocation == null) {
curLocation = location;
locationChanged = true;
}else if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()){
locationChanged = false;
return;
}else
locationChanged = true;

curLocation = location;

if (locationChanged)
locMan.removeUpdates(gpsListener);

}
public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status,Bundle extras) {
if (status == 0)// UnAvailable
{
} else if (status == 1)// Trying to Connect
{
} else if (status == 2) {// Available
}
}

};

@Override
public void onCreate() {
super.onCreate();

curLocation = getBestLocation();

if (curLocation == null)
Toast.makeText(getBaseContext(),"Unable to get your location", Toast.LENGTH_SHORT).show();
else{
//Toast.makeText(getBaseContext(), curLocation.toString(), Toast.LENGTH_LONG).show();
}

isService = true;
}
final String TAG="LocationService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onLowMemory() {
super.onLowMemory();
}

@Override
public void onStart(Intent i, int startId){
handler.postDelayed(GpsFinder,1);
}

@Override
public void onDestroy() {
handler.removeCallbacks(GpsFinder);
handler = null;
Toast.makeText(this, "Stop services", Toast.LENGTH_SHORT).show();
isService = false;
}

public IBinder onBind(Intent arg0) {
return null;
}

public Runnable GpsFinder = new Runnable(){
public void run(){

Location tempLoc = getBestLocation();
if(tempLoc!=null)
curLocation = tempLoc;
tempLoc = null;
handler.postDelayed(GpsFinder,1000);// register again to start after 1 seconds...
}
};

private Location getBestLocation() {
Location gpslocation = null;
Location networkLocation = null;

if(locMan==null){
locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
}
try {
if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);// here you can set the 2nd argument time interval also that after how much time it will get the gps location
gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

}
if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener);
networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} catch (IllegalArgumentException e) {
//Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString());
Log.e("error", e.toString());
}
if(gpslocation==null && networkLocation==null)
return null;

if(gpslocation!=null && networkLocation!=null){
if(gpslocation.getTime() < networkLocation.getTime()){
gpslocation = null;
return networkLocation;
}else{
networkLocation = null;
return gpslocation;
}
}
if (gpslocation == null) {
return networkLocation;
}
if (networkLocation == null) {
return gpslocation;
}
return null;
}
}

您可以将时间设置到处理程序或 requestLocationUpdates() 中。您需要在家中启动此服务。至于我已经为处理程序和 requestLocationUpdate() 设置了 1 秒,以便在 1 秒后获取位置并更新我。

已编辑:作为获取用户当前位置的服务,您可以从家庭 Activity 开始,也可以从开机时间开始。通过 home activity 确保如果服务被用户使用其他应用程序(如任务 killer )停止,那么当用户启动您的应用程序时,该服务将在家中再次启动,要启动服务,您可以这样做

startService(new Intent(YourActivity.this,ServiceLocation.class));

当您需要停止服务时,它会调用 onDestroy() 以便处理程序可以取消线程以继续获取位置。

由于 GPS 设置为 1 秒(1000),这将每 1 秒获取一次 gps 位置,但要获取该位置,您需要每次都调用,在我的情况下,我已设置为 1 秒,并根据您的要求设置到 30 秒。所以你 gps 每 1 秒获取一次位置并在处理程序中使用此服务设置 30 分钟。为了节省电池生命周期,您还可以在 gps 请求方法中设置不同的时间,这样可以节省电池生命周期。

您可以删除位置和当前位置的比较部分,因为 locationlistener 每次在位置更改时调用,您可以在应用程序的任何位置使用 curLocation 来获取当前位置,但首先要确保您必须启动此服务first then after you can use otherwise you get null pointer exception when you access the latitude and longitude of this object

希望你有一些想法,我得到了关于你的问题的答案

关于android - 在特定时间间隔后获取当前设备位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8352420/

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