2010-08-28 12 views

Répondre

10

Vous devez définir un onClickpendingIntent sur votre widget

Intent intent = new Intent(context, ExampleActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
// Get the layout for the App Widget and attach an on-click listener to the button 
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget_provider_layout); 
views.setOnClickPendingIntent(R.id.button, pendingIntent); 

Check this out

Processing more than one button click at Android Widget

+0

Premier lien mort. –

+0

Question secondaire, pouvez-vous extraire des activités pour une application spécifique? (peut-être je ne sais pas quelle classe est ExampleActivity.class) – RelativeGames

5

Incluez ce code dans la méthode onUpdate() de la classe WidgetProvider.

for(int j = 0; j < appWidgetIds.length; j++) 
{ 
    int appWidgetId = appWidgetIds[j]; 

    try { 
     Intent intent = new Intent("android.intent.action.MAIN"); 
     intent.addCategory("android.intent.category.LAUNCHER"); 

     intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
     intent.setComponent(new ComponentName("your Application package", 
      "fully qualified name of main activity of the app")); 
     PendingIntent pendingIntent = PendingIntent.getActivity(
      context, 0, intent, 0); 
     RemoteViews views = new RemoteViews(context.getPackageName(), 
      layout id); 
     views.setOnClickPendingIntent(view Id on which onclick to be handled, pendingIntent); 
    appWidgetManager.updateAppWidget(appWidgetId, views); 
    } catch (ActivityNotFoundException e) { 
      Toast.makeText(context.getApplicationContext(), 
        "There was a problem loading the application: ", 
        Toast.LENGTH_SHORT).show(); 
    } 

} 
+0

Works Amazing! Merci Shivanand Darur –

+3

"votre paquet d'application", "nom complet de l'activité principale de l'application" signifie 'intent.setComponent (nouveau ComponentName (context.getPackageName(), MainActivity.class.getName())' –

Questions connexes