2010-05-31 39 views
1

我無法啓動服務來更新我創建的AppWidget作爲練習。我試圖從DDMS中獲取欺騙性位置數據的經度和緯度以顯示在小部件中。該小部件使用一個服務來更新TextView,這可能稍微有點矯枉過正,但我​​想要遵循這個模板,這在AppWidgets中似乎很常見,可以完成更多工作(如預測小部件或維基文庫小部件)。按鈕點擊無法啓動Android應用程序小部件中的服務

現在,我沒有收到任何錯誤消息或奇怪的行爲;按下按鈕時根本沒有任何事情發生。對於什麼可能是錯誤的,我有點神祕。任何人都可以指出我正確的方向嗎?

另外,如果我的位置邏輯有問題,我也會喜歡這方面的建議。我查看了幾個博客,Google示例和文檔,但我對它的工作原理感到有些模糊。

這裏是小工具的當前狀態:

public class Widget extends AppWidgetProvider 
{ 
    static final String TAG = "Widget"; 
    /** 
    * {@inheritDoc} 
    */ 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
         int[] appWidgetIds) 
    { 
     // Create an intent to launch the service 
     Intent serviceIntent = new Intent(context, UpdateService.class); 

     // PendingIntent is required for the onClickPendingIntent that actually 
     // starts the service from a button click 
     PendingIntent pendingServiceIntent = 
      PendingIntent.getService(context, 0, serviceIntent, 0); 

     // Get the layout for the App Widget and attach a click listener to the 
     // button 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main); 
     views.setOnClickPendingIntent(R.id.address_button, pendingServiceIntent); 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
    } 

    // To prevent any ANR timeouts, we perform the update in a service; 
    // really should have its own thread too 
    public static class UpdateService extends Service 
    { 
     static final String TAG = "UpdateService"; 
     private LocationManager locationManager; 
     private Location currentLocation; 
     private double latitude; 
     private double longitude; 

     public void onStart(Intent intent, int startId) 
     { 
      // Get a LocationManager from the system services 
      locationManager = 
       (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      // Register for updates from spoofed GPS 
      locationManager.requestLocationUpdates("gps", 30000L, 0.0f, new LocationListener() 
      { 
       @Override 
       public void onLocationChanged(Location location) 
       { 
        currentLocation = location; 
       } 

       @Override 
       public void onProviderDisabled(String provider) {} 

       @Override 
       public void onProviderEnabled(String provider) {} 

       @Override 
       public void onStatusChanged(String provider, int status, 
         Bundle extras) {}  
      }); 
      // Get the last known location from GPS 
      currentLocation = 
       locationManager.getLastKnownLocation("gps"); 

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

      // Push update for this widget to the home screen 
      ComponentName thisWidget = new ComponentName(this, Widget.class); 

      // AppWidgetManager updates AppWidget state; gets information about 
      // installed AppWidget providers and other AppWidget related state 
      AppWidgetManager manager = AppWidgetManager.getInstance(this); 

      // Updates the views based on the RemoteView returned from the 
      // buildUpdate method (stored in updateViews) 
      manager.updateAppWidget(thisWidget, updateViews); 
     } 

     public RemoteViews buildUpdate(Context context) 
     { 
      latitude = currentLocation.getLatitude(); 
      longitude = currentLocation.getLongitude(); 
      RemoteViews updateViews = 
       new RemoteViews(context.getPackageName(), R.layout.main); 
      updateViews.setTextViewText(R.id.latitude_text, "" + latitude); 
      updateViews.setTextViewText(R.id.longitude_text, "" + longitude); 
      return updateViews; 
     } 

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

} 

回答

4

嘗試設置的

PendingIntent.getService(context, 0, serviceIntent, 0); 

最後一個參數:

PendingIntent.getService(context, 0, serviceIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

而且有時你需要addData到您的意圖讓Android將它區分爲新的東西,這有點不好意思,但似乎可以這樣工作:

serviceIntent.setData(Uri.parse("uri::somethingrandomandunique"); 
0
PendingIntent pendingIntent = PendingIntent.getService(context, 0, startServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    remoteViews.setOnClickPendingIntent(R.id.start_service_btn_widget, pendingIntent); 

appWidgetManager.updateAppWidget一定要打電話,或點擊事件不響應

appWidgetManager.updateAppWidget(componentName, remoteViews); 
相關問題