2012-12-16 72 views
1

有很多有關的LocationManager問題,並獲得用戶的位置的不同方法。但是,其中大部分都集中在準確性上,而且由於要求不同,我的方法有點不同。 我需要的是約。用戶的GPS位置儘可能快(但僅在用戶點擊按鈕後),並且一些EditText框應填充座標。問題是,在大多數情況下,設備上的數據傳輸將被禁用,因此網絡位置將不可用。我必須堅持不能通過設計快速的GPS。所以這裏是我做的:如何儘可能取消LocationManager.requestSingleUpdate?

按鈕單擊我打電話給我的主要活動getGPS()方法,因爲我不關心準確性(我只需要獲得一個大約座標)我檢查最後已知的位置,代碼中的註釋是不言自明

public void getGPS(View view) { 
    // load all available Location providers 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    List<String> providers = locationManager.getProviders(false); 
    // determine the last known location within 2 hours available from cache 
    Location myLocation = null; 
    Date now = new Date(); 
    long time = now.getTime(); 
    long timeDiff = LOCATION_MAX_AGE; 
    for (int i=providers.size()-1; i>=0; i--) { 
     Location l = locationManager.getLastKnownLocation(providers.get(i)); 
     if (l != null) { 
      long t = l.getTime(); 
      if (time - t < timeDiff) { 
       myLocation = l; 
       time = t; 
       timeDiff = time - t; 
      } 
     } 
    } 
    // if failed to get cached location or if it is older than 2 hours, request GPS position 
    if (myLocation == null) { 
     if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      pIntent = PendingIntent.getBroadcast(context, 
        0, 
        new Intent(SINGLE_UPDATE_ACTION), 
        PendingIntent.FLAG_UPDATE_CURRENT); 

      IntentFilter iFilter = new IntentFilter(SINGLE_UPDATE_ACTION); 
      receiver = new BroadcastReceiver() { 
       @Override 
       public void onReceive(Context context, Intent intent) { 
        // when received an answer from the LocationManager 
        Location gpsLocation = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED); 
        fillLocation(gpsLocation); 
        Log.d(TAG, "Received GPS location: " + gpsLocation.getLatitude() + ", " + gpsLocation.getLongitude() + " Time: " + gpsLocation.getTime()); 
        locationManager.removeUpdates(pIntent); 
        unregisterReceiver(receiver); 
       } 
      }; 
      context.registerReceiver(receiver, iFilter); 
      // I'm absolutely fine with getting the first available GPS position and that's enough to me. 
      locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, pIntent); 
     // if GPS is disabled in settings allow user to enable it 
     } else { 
      showGPSDisabledAlertToUser(); 
     } 
    } else { 
     // if fresh enough lastKnownLocation is found 
     fillLocation(myLocation); 
    } 
} 

所有工作得非常好,在大多數情況下是有lastKnownLocation和座標顯示用戶點擊按鈕後即可。但是當我不那麼幸運時,會出現GPS圖標,問題就來了。由於沒有可用的數據連接,所以GPS修復比平常花費的時間更長。在此期間,活動仍然可以訪問,並且用戶並沒有真正意識到座標即將出現。另一個問題是,當用戶無法在合理的時間內獲得GPS定位時,他應該能夠取消該請求。

所以,我的問題是:什麼在這裏是最好的方法? 更具體:是否有可能表現出像一個等待對話框,用戶(例如「獲取GPS位置......」)和一鍵式「取消」,這可能會中斷請求,並把用戶返回到(如何?)主要活動,停止任何GPS活動並顯示敬酒要求用戶手動輸入座標?

很抱歉,如果這是一件很容易的,我剛開始學的Android開發和我的應用程序,除了這件事情幾乎已經完成。我很努力地搜索,沒有發現任何不幸的事情。提前謝謝了!

編輯:只是一個半小時,我貼我的問題後,我意識到,我可能已經知道答案。我可能需要打開一個等待光標的對話框來阻止用戶界面,顯示我的消息和取消按鈕並從那裏請求LocationManager。如果用戶在我們修復之前點擊取消,我應該取消註冊我的接收器並關閉對話框。 當然,我會明天嘗試這個,但任何想法或建議,甚至評論都非常歡迎!

回答

0

我建議你使用ProgressDialog通知正在等待修復用戶。您可以設置該消息並添加一個Cancel按鈕。看到這個鏈接:How to set cancel button in Progress Dialog?

如果用戶取消ProgressDialod,那麼您將啓動屏幕,請求手動輸入。

問候。

+0

路易斯,謝謝,這正是我昨天想到的(請參閱我的編輯),但我肯定接受你的答案,因爲它爲我節省了一些時間,找到了對話類的正確名稱:) – remarkov

0

由於路易斯回答我二次的問題(這基本上是取消請求「什麼時候?」)我將答案添加到我的第一和主要問題(更容易一個雖然):是的,這是可以取消的GPS搜索在requestSingleUpdate方法調用之後,如果您只是取消註冊偵聽器並刪除更新。我在取消按鈕的onClick方法中執行此操作。有一點要記住,以及:調用cancel()方法將ProgressDialog如果GPS得到了修復和恢復的位置。