如何通过单击小部件打开应用程序

时间:2014-02-18 21:44:42

标签: android android-widget widget android-appwidget

我的WidgetProvider因此:

package br.com.pedro.widget;

import android.appwidget.AppWidgetProvider;

public class MyWidgetProvider extends AppWidgetProvider {

}

如何显示一个显示布局的窗口小部件,当点击它时会转到我的主类?

1 个答案:

答案 0 :(得分:1)

此处将MainActivity替换为您要打开的活动。

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        Toast.makeText(context, "Touched view ", Toast.LENGTH_SHORT).show();
        Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds).size());
        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, MainActivity.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.widget_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

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