How to launch activity from android home screen widget

百般思念 提交于 2019-11-28 12:18:36

Well, your app widget should already have a PendingIntent that you tied to the button. Instead of a PendingIntent that triggers a BroadcastReceiver, have it be a PendingIntent that starts up an Activity.

Saugat man ligal

You can use this code to solve your problem.

public class Widget extends AppWidgetProvider {

        // ...

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

            for(int i = 0; i < appWidgetIds.length; i++){

                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);

                Intent startActivityIntent = new Intent(context, myActivity.class);
                PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                widget.setPendingIntentTemplate(R.id.list_view, startActivityPendingIntent);

                appWidgetManager.updateAppWidget(appWidgetIds[i], widget);

                // ...
        }
    }

    public class WidgetAdapter implements RemoteViewsService.RemoteViewsFactory {

        // ...

        @Override
        public RemoteViews getViewAt(int position) {

        RemoteViews widgetRow = new RemoteViews(context.getPackageName(), R.layout.widget_row);

            Intent fillInIntent = new Intent();
            fillInIntent.putExtra(Widget.EXTRA_LIST_VIEW_ROW_NUMBER, position);
            widgetRow.setOnClickFillInIntent(R.id.list_view_row, fillInIntent);

            // ...

            return row;
        }
    }

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!