如何在保留在主屏幕上的小部件中打开另一个视图?

时间:2018-07-15 07:51:46

标签: android android-intent android-widget

我在使用《卫报》新闻应用程序时,发现他们的窗口小部件具有一个选项按钮,可以打开其他菜单。我不确定如何做到这一点。如果有人有什么好主意。

Guardian Widget

Options menu open

2 个答案:

答案 0 :(得分:1)

通过这种轻松的方式,它可以打开对话框或模式弹出窗口活动。

答案 1 :(得分:0)

您可以照常发送Intent

    public class YourWidget extends AppWidgetProvider {

    static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
        KLog.d(TAG, "getPendingSelfIntent appWidgetId = " + appWidgetId);
        Intent intent = new Intent(context, YourWidget.class);
        Bundle b = new Bundle();
        b.putInt("appWidgetId", appWidgetId);
        intent.putExtras(b);
        intent.setAction(action);
        PendingIntent pendInt = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendInt;
    }

    //....

    //your method for remote views 
    public void doUpdateView() {
         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.collection_widget);
         remoteViews.setOnClickPendingIntent(R.id.button_settings, getPendingSelfIntent(context, RUN_CLICKED, appWidgetId));
   }

    @Override
    public void onReceive(final Context context, Intent intent) {
        super.onReceive(context, intent);
        if (RUN_CLICKED.equals(intent.getAction())) {
            Intent actIntent = new Intent(context, YourActivity.class);
            actIntent.putExtra("your_extra_key", your_extra_value);
            actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(actIntent);
        } 
        //...
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
    }