我正在開發時正常工作的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