2009-11-26 156 views
3

我正在使用一個Widget,它將獲取當前GPS位置並將此值傳遞給遠程PHP頁面以獲取信息並將其顯示在Widget中。這是我想要做的。在android appwidget中實現位置監聽器時出現問題

我在爲appWidget實現Location Listener時遇到問題。它不是用當前位置進行更新,而是顯示最初的小部件,即「加載小部件」(這裏我把這個文本)

我們可以爲AppWidgetProvider實現位置監聽器嗎?

你能否建議我在App Widget中獲取GPS位置的可能解決方案?

我把所有必要的權限放在清單文件中。
這裏是我的代碼片段:

public class test extends AppWidgetProvider { 
static LocationManager locMan; 
static Location curLocation; 
static Boolean locationChanged; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) { 
    // To prevent any ANR timeouts, we perform the update in a service 
    context.startService(new Intent(context, UpdateService.class)); 
} 

public static class UpdateService extends Service { 
    // GPS Listener Class 
    LocationListener gpsListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      // Log.w("GPS", "Started"); 
      if (curLocation == null) { 
       curLocation = location; 
       locationChanged = true; 
      } 

      if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()) 
       locationChanged = false; 
      else 
       locationChanged = true; 

      curLocation = location; 

      if (locationChanged) 
       locMan.removeUpdates(gpsListener); 

     } 

     public void onProviderDisabled(String provider) { 

     } 

     public void onProviderEnabled(String provider) { 
      // Log.w("GPS", "Location changed", null); 
     } 

     public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
      if (status == 0)// UnAvailable 
      { 

      } else if (status == 1)// Trying to Connect 
      { 

      } else if (status == 2) {// Available 

      } 
     } 

    }; 

    // In service start method, I am registering for GPS Updates 
    @Override 
    public void onStart(Intent intent, int startId) { 

     locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener); 
     } else { 
      this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS")); 
     } 

     if (curLocation != null) { 
      double lat = curLocation.getLatitude(); 
      double lng = curLocation.getLongitude(); 
      Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show(); 

     } 
     // Build the widget update for today 
     RemoteViews updateViews = buildUpdate(this); 

     // Push update for this widget to the home screen 
     ComponentName thisWidget = new ComponentName(this,InKakinadaWidget.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(thisWidget, updateViews); 

    } 

    public RemoteViews buildUpdate(Context context) { 
     // Here I am updating the remoteview 
     return updateViews; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // We don't need to bind to this service 
     return null; 
    } 

} 
} 

在此先感謝...

與問候, Raghavendra K.

回答

3

你是否有在模擬器或設備上這個問題呢?如果在模擬器上,請確保使用DDMS來設置GPS位置。但最簡單的做法可能是在設備上運行代碼,以查看是否以這種方式獲取GPS更新。

+0

嗨邁克, 我在我的HTC英雄測試。但沒有運氣。我的代碼有問題嗎? 還是你可以建議我任何其他方式來獲得gps位置的Widget? 感謝您的快速反應... – Raghu 2009-11-26 11:27:32

+0

我很樂意提供幫助,但我對小部件不是很熟悉。我忘記提及的一件事就是確保你使用設備在外面走動 - 過去我一直在努力處理代碼,只是意識到設備無法在室內獲得GPS定位,因此不會發送給我GPS更新。 – emmby 2009-11-26 17:22:21

8

我看到的問題是您正在嘗試在調用requestLocationUpdates()後立即檢查onStart()中的curLocation。 LocationManager.requestLocationUpdates()是異步的,這意味着在onStart()返回之前,您的LocationListener不會被回調。讓LocationListener在接收到位置後更新UI,請勿嘗試在onStart中執行此操作。

+0

嗨布萊恩, 你是對的。我遇到了我給AppWidget(20秒)和位置偵聽器(2秒)提供的時間間隔的問題。然後兩者不會相互同步。我將AppWidget的時間間隔設置爲1分鐘,將位置監聽器設置爲20秒。現在它工作正常... 非常感謝您的指導... – Raghu 2009-11-30 03:58:08

+0

樂於幫助。如果你有一分鐘​​,也許將這個答案標記爲正確答案。 – 2009-11-30 16:46:48