最小化并打开另一个应用程序时中断android应用程序
我正在开发一个GPS应用程序,当它启动时工作正常,但当我最小化它并打开其他应用程序时,它不会连续给出纬度和经度,当我关闭其他应用程序并运行此单一应用程序时,它会给出准确的结果。如果有人知道的话,请帮帮我 您应该使用服务在后台运行。当您打开其他应用程序时,gps应用程序将以暂停方式消失。在停止方法之后。当您重新打开它时,它将调用restart方法最小化并打开另一个应用程序时中断android应用程序,android,Android,我正在开发一个GPS应用程序,当它启动时工作正常,但当我最小化它并打开其他应用程序时,它不会连续给出纬度和经度,当我关闭其他应用程序并运行此单一应用程序时,它会给出准确的结果。如果有人知道的话,请帮帮我 您应该使用服务在后台运行。当您打开其他应用程序时,gps应用程序将以暂停方式消失。在停止方法之后。当您重新打开它时,它将调用restart方法 因此,我认为您必须使用gps lat和lng服务。服务将在后台持续运行。很好!它的发生仅仅是因为你在后台模式下的活动。当最小化应用程序时,这意味着调用活
因此,我认为您必须使用gps lat和lng服务。服务将在后台持续运行。很好!它的发生仅仅是因为你在后台模式下的活动。当最小化应用程序时,这意味着调用活动停止方法,然后停止活动 解决方案 对于这种情况,您必须像下面的示例一样使用服务
public class LocationService extends Service implements LocationListener{
//public static final String BROADCAST_ACTION = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public Location previousBestLocation = null;
Intent intent;
int counter = 0;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
//intent = new Intent(BROADCAST_ACTION);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20000, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 0, this);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected boolean isBetterLocation(Location location,Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be
// worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(this);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally { }
}
};
t.start();
return t;
}
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
Log.i("***", "Location changed");
if (isBetterLocation(loc, previousBestLocation)) {
double latI = loc.getLatitude();
double longI = loc.getLongitude();
//Put Data into shared prefrence
SharedPreferences sharedPreferences = getSharedPreferences(ApplicationCardinality.PREF_NAME, MODE_PRIVATE);;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("gpsLatitude", ""+latI);
editor.putString("gpsLongitude", ""+longI);
editor.commit();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
发布你的代码你做了什么!我在Genymotion测试的时候不是这样的,它工作得很好。。但我在安卓手机上测试过。。。如果没有像Facebook和chrome这样的应用程序打开,现在就开始并最小化。。然后结果就被打断了。。。