有趣的小部件难题

时间:2012-08-15 15:49:40

标签: android android-intent widget

我有一个小部件,主要做我想要的(开始一个意图),但我想知道如何将多个意图压缩到一个小部件提供者。

我已经尝试了所有通常的方法来完成这项工作:

  • 使用多个意图
  • 使用具有相同主题设置的第二个小部件 以及其他一些最终失败的人。

通用代码:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {

        Intent startIntent = new Intent(context, myClass.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startIntent, 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.MyWidget);
        views.setOnClickPendingIntent(R.id.startBtn, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current App Widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }       
}

所以我想弄清楚如何在小部件上获得更多的按钮。由于它设置为三个,我知道其中一个工作,所以我是那里的1/3。

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/startBtn"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_marginBottom="5dp"
    android:background="@drawable/mybg"
    android:gravity="center"
    android:text="First Menu Item"
     />

<TextView
    android:id="@+id/startBtn2"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_marginBottom="5dp"
    android:background="@drawable/mybg"
    android:gravity="center"
    android:text="Second menu item" />

<TextView
    android:id="@+id/startBtn3"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_marginBottom="5dp"
    android:background="@drawable/mybg"
    android:gravity="center"
    android:text="Third menu item" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

好的,所以你几乎得到了它!我们的想法是创建3个不同的PendingIntents和3个不同的Intents,然后调用setOnClickPendingIntent 3次。每个动作和按钮一个。这是一个例子,假设三个活动是myClass,myClass2和myClass3。使用下面的代码,每个按钮将触发不同的意图。

for (int appWidgetId : appWidgetIds) {

    Intent startIntent = new Intent(context, myClass.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startIntent, 0);

    Intent startIntent2 = new Intent(context, myClass2.class);
    PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, startIntent2, 0);

    Intent startIntent3 = new Intent(context, myClass3.class);
    PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 0, startIntent3, 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.MyWidget);
    views.setOnClickPendingIntent(R.id.startBtn, pendingIntent);
    views.setOnClickPendingIntent(R.id.startBtn2, pendingIntent2);
    views.setOnClickPendingIntent(R.id.startBtn3, pendingIntent3);


    // Tell the AppWidgetManager to perform an update on the current App Widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}