2010-05-31 4 views
1

J'ai des difficultés à démarrer un service pour mettre à jour un AppWidget que je crée en tant qu'exercice. J'essaie d'obtenir la latitude et la longitude des données de localisation falsifiées de DDMS à afficher dans le widget. Le widget utilise un service pour mettre à jour le TextView, qui peut être légèrement exagéré, mais je voulais suivre le modèle qui semble être commun dans AppWidgets qui fait plus de travail (comme le widget Forecast ou le widget Wiktionary).Le clic sur le bouton ne démarre pas Service dans l'application Widget Android

À l'heure actuelle, je ne reçois aucun message d'erreur ou comportement étrange; rien ne se passe quand on appuie sur le bouton. Je suis un peu mystifié quant à ce qui pourrait être faux. Quelqu'un pourrait-il me pointer dans la bonne direction?

De plus, si ma logique de localisation est défectueuse, j'aimerais aussi avoir des recommandations à ce sujet. J'ai regardé plusieurs blogs, les exemples de Google, et la documentation, mais je me sens un peu flou sur la façon dont cela fonctionne.

Voici l'état actuel du widget:

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; 
     } 
    } 

} 

Répondre

4

Essayez le réglage du dernier paramètre de

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

à:

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

Aussi, vous avez parfois besoin addData à votre intention pour Android de le distinguer comme quelque chose de nouveau, c'est un peu hacky mais semble fonctionner donc ajouter:

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 doit être appeler, ou cliquez sur l'événement ne sera pas une réponse

appWidgetManager.updateAppWidget(componentName, remoteViews); 
Questions connexes