我已經搜索了很多,我並不完全無知。我已經實施了一個臨時解決方案,但是想知道是否有更好的方法。將位置信息提供給服務的最佳方式
我有一個應用程序,每60秒後將一個人的位置發送到服務器。在我的儀表盤(主屏幕應用程序啓動後,將去的onPause),我已經註冊了的LocationManager用下面的代碼:
service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
else
{
Criteria criteria = new Criteria();
provider = service.getBestProvider(criteria, false);
service.requestLocationUpdates(provider, 10000, 50, this);
Location location = service.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null)
{
onLocationChanged(location);
}
else
{
Log.d("Location: ", "No update received");
}
}
然而,正如我所說,本次活動將被最小化用戶(通過按主頁按鈕)。 AlarmManager每60秒會調用一次服務。該服務從Dashboard Activity(lat,lon)訪問靜態變量並將其發送到服務器。
我的問題:
如果活動去的onPause,將requestLocationUpdates功能停止?或者它會繼續工作?
如果它繼續工作,它將不斷更新兩個經緯度和靜態字符串對象,並且服務將不斷獲取更新值。如果他們停下來,服務會一直獲得相同的舊值。
此外,有沒有更好的方法來解決這個問題?使用GPS提供商和網絡提供商的混合? (我需要相當準確的值)。
編輯
這裏是我的報警。此代碼是內部登錄活動
Intent i = new Intent(con, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(con,
Login.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER,
LocationManager.GPS_PROVIDER);
gps = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(con, 0, i, 0);
gps.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
10 * 1000, pi);
Log.d("Service: ",
"GPS Service started and scheduled with AlarmManager");
這裏是我的接收器(也內登錄活動)
private class ReceiveMessages extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Location loc = (Location) intent.getExtras().get(
LocationPoller.EXTRA_LOCATION);
String msg;
if (loc == null)
{
msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
}
else
{
msg = loc.toString();
}
if (msg == null)
{
msg = "Invalid broadcast received!";
}
Log.d("GPS Broadcast: ", msg);
}
}
沒有發生的事情:不是過得好logcat的,這意味着沒有被接收的廣播東西。
這個例子是這樣複雜的微不足道,因爲這一個任務。 – Asim
@Asim - 我假設你想持續獲取位置並更新到服務器。如果是這樣的話,那麼你需要這種複雜性。例如,如果手機處於睡眠模式並且您想上傳位置信息。爲此,您需要獲取喚醒鎖定,獲取並上傳位置,然後釋放喚醒鎖定。如果您的需要只是在活動處於前臺時獲取並更新位置,則可以通過從您的活動獲取位置並將其更新到服務器的服務啓動服務 – Amit
確定我已導入庫。我現在有點困惑(全新的BroadcastReceiver)。我正在登錄屏幕上啓動鬧鐘。該屏幕在用戶登錄時(用戶登陸儀表板)進入onResume(或其他)。 RECEIVER應該進行哪項活動?此外,我的儀表板活動已在使用接收器進行其他任務。如何區分多個廣播?我知道intents ..但是這個廣播的初衷是Intent(Login,Dashboard),但是我怎樣才能在我的Dashboard活動中使用它? 對不起,如果這是所有非常混淆:) – Asim