我正在开发时正常工作的GPS应用程序,但是当我将其最小化并打开其他某个应用程序时,它不会连续提供经度和纬度,并且在关闭其他应用程序时应用程序并运行这个单一的应用程序,然后它给出准确的结果任何人有任何想法然后请帮助我..当它最小化并打开另一个应用程序时中断了Android应用程序
0
A
回答
0
你应该使用service
在后台运行。当你打开其他应用程序,而不是它的GPS应用程序将停止暂停方法。之后停止方法。当你重新打开它时,它会调用重启方法。
所以我认为你必须使用gps lat和lng服务。服务将不断在后台运行。
0
好吧!这仅仅是因为你在后台模式下的活动。当你最小化你的应用程序时,意味着调用了stop()方法,然后停止了你的Activity。
解决方案
对于这种情况,你必须使用该服务这样的例子
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) { }
}
+0
没有!不是这样的时候,我在Genymotion测试它运行良好..但我测试在Android移动然后...开始和最小化,现在如果没有任何应用程序像Facebook和铬打开..然后结果被打断... – Naresh 2014-10-31 07:24:09
相关问题
- 1. 当应用程序不应该打开时最大化应用程序打开
- 2. 当另一个应用程序被点击时打开一个应用程序
- 3. 在另一个应用程序中打开一个Android应用程序
- 4. 当另一个应用程序关闭时打开应用程序
- 5. 当屏幕保护程序打开时,全屏java应用程序最小化
- 6. 从我的应用程序打开另一个应用程序?
- 7. 如何从另一个Android Xamarin应用程序打开Android Xamarin应用程序?
- 8. 从另一个程序中打开一个应用程序
- 9. 如何从Android的另一个应用程序中打开应用程序?
- 10. 关闭delphi应用程序以及它打开的另一个应用程序
- 11. 打开一个应用程序从我的Android应用程序
- 12. 当我打开另一个活动时,应用程序崩溃
- 13. 从Android的另一个应用程序打开默认日历应用程序
- 14. android:我如何从我的应用程序打开另一个应用程序?
- 15. Android NullPointerException当我的应用程序长时间最小化时
- 16. TransactionTooLargeException当最小化应用程序
- 17. Android应用程序崩溃时,另一个打开活动
- 18. 在应用程序最小化时打开/关闭手电筒
- 19. 从另一个应用程序中打开一个应用程序而不关闭应用程序
- 20. 当一个应用程序尝试连接到另一个应用程序时,断开的管道异常
- 21. 保持我的Android应用程序最小化,一旦应用程序被永久打开一次
- 22. 从另一个应用程序打开TestFlight应用程序并深入链接到特定应用程序
- 23. Android最小化应用程序
- 24. 最小化应用程序android
- 25. Android - 应用程序最小化处理
- 26. Android应用程序最小化并重新对焦后,Android应用程序从开始处开始。
- 27. Chrome打包的应用程序:打开另一个应用程序
- 28. 从另一个中打开另一个JavaFX应用程序?
- 29. 打开另一个应用程序从反应原生的iOS应用程序
- 30. 如何从android中的另一个apllication打开应用程序?
发布您的代码你做了什么 – 2014-10-31 05:55:50