質問: locationrequest.create(); locationrequest.setinterval(10000); locationclient.requestlocationupdates(locationrequest,new com.google.android.gms.location.locationlistener() { @override public void onlocationchanged(location location) { log.i(tag, "last known location :" + location.getlatitude() + "," + location.getlongitude()); } }); 今から私はこのコードを呼び出す必要があります。私はこれをバックグラウンドサービスまたは他の場所で使用できますか。あなたのアイデアを教えてください。tia.
回答: import android.app.service; import android.content.context; import android.content.intent; import android.hardware.geomagneticfield; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.ibinder; import android.provider.settings; import android.widget.toast; public class locationservice extends service { private static final int minutes = 1000 * 60 * 2; public locationmanager locationmanager; public mylocationlistener listener; public location previousbestlocation = null; context mcontext; private boolean isgpsenabled = false; private boolean isnetworkenabled = false; private geomagneticfield geofield; private double latitude = 0.0; private double longitude = 0.0; @override public void oncreate() { super.oncreate(); mcontext = getapplicationcontext(); if (locationmanager == null) { locationmanager = (locationmanager) mcontext.getsystemservice(context.location_service); } getcurrentlocation(); } private void getcurrentlocation() { try { assert locationmanager != null; isgpsenabled = locationmanager.isproviderenabled(locationmanager.gps_provider); isnetworkenabled = locationmanager.isproviderenabled(locationmanager.network_provider); } catch (exception ex) { ex.printstacktrace(); } if (!isgpsenabled && !isnetworkenabled) { showsettingsalert(); } listener = new mylocationlistener(); try { locationmanager.requestlocationupdates(locationmanager.gps_provider, 10000, 0, listener); locationmanager.requestlocationupdates(locationmanager.network_provider, 10000, 0, listener); } catch (securityexception e) { e.printstacktrace(); } } private void showsettingsalert() { toast.maketext(mcontext, "gps is disabled in your device. please enable it ?", toast.length_long).show(); intent intent = new intent(settings.action_location_source_settings); intent.addflags(intent.flag_activity_new_task); mcontext.startactivity(intent); } @override public int onstartcommand(intent intent, int flags, int startid) { return start_sticky; } @override public ibinder onbind(intent intent) { return null; } protected boolean isbetterlocation(location location, location currentbestlocation) { if (currentbestlocation == null) { return true; } long timedelta = location.gettime() - currentbestlocation.gettime(); boolean issignificantlynewer = timedelta > minutes; boolean issignificantlyolder = timedelta < -minutes; boolean isnewer = timedelta > 0; if (issignificantlynewer) { return true; } else if (issignificantlyolder) { return false; } int accuracydelta = (int) (location.getaccuracy() - currentbestlocation.getaccuracy()); boolean islessaccurate = accuracydelta > 0; boolean ismoreaccurate = accuracydelta < 0; boolean issignificantlylessaccurate = accuracydelta > 200; boolean isfromsameprovider = issameprovider(location.getprovider(), currentbestlocation.getprovider()); if (ismoreaccurate) { return true; } else if (isnewer && !islessaccurate) { return true; } else if (isnewer && !issignificantlylessaccurate && isfromsameprovider) { return true; } return false; } private boolean issameprovider(string provider1, string provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } @override public void ondestroy() { super.ondestroy(); locationmanager.removeupdates(listener); } private void setupfinallocationdata(location mlocation) { if (mlocation != null) { geofield = new geomagneticfield( double.valueof(mlocation.getlatitude()).floatvalue(), double.valueof(mlocation.getlongitude()).floatvalue(), double.valueof(mlocation.getaltitude()).floatvalue(), system.currenttimemillis() ); latitude = mlocation.getlatitude(); longitude = mlocation.getlongitude(); //update latitude and longtitude in sharedpreference... } } public class mylocationlistener implements locationlistener { public void onlocationchanged(final location loc) { if (isbetterlocation(loc, previousbestlocation)) { setupfinallocationdata(loc); } } public void onproviderdisabled(string provider) { } public void onproviderenabled(string provider) { } public void onstatuschanged(string provider, int status, bundle extras) { } } }