【问题标题】:How to open application by clicking widget如何通过单击小部件打开应用程序
【发布时间】:2014-02-18 21:44:42
【问题描述】:

我的 WidgetProvider 是这样的:

package br.com.pedro.widget;

import android.appwidget.AppWidgetProvider;

public class MyWidgetProvider extends AppWidgetProvider {

}

如何显示一个显示布局的小部件,并在单击时转到我的主类?

【问题讨论】:

    标签: android android-widget widget android-appwidget


    【解决方案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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多