简体   繁体   English

如何在单击android中的小部件按钮时打开一个对话框

[英]How to open a dialog box on the click of a widget button in android

I am trying to create a button in my app's widget.我正在尝试在我的应用程序小部件中创建一个按钮。 I expect it to open a dialog box when clicked.我希望它在单击时打开一个对话框。 I am unsure of how to implement this.我不确定如何实现这一点。 Here is the widget's code:这是小部件的代码:

public class IncompleteTodosWidget extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        Intent intent = new Intent(context, timeTypeSelector.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                /* context = */ context,
                /* requestCode = */ 0,
                /* intent = */ intent,
                /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.incomplete_todos_widget);
        views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);


        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

}

Here is the code of the dialog box:下面是对话框的代码:

public class timeTypeSelector extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Demo message")
                .setPositiveButton("Perform miracles", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Log.d("Bitcoin has reached 69,42,000");
                    }
                })
                .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Log.d("Bitcoin would have reached 69,42,000 if you had clicked the other button");
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

When I try clicking the view with id appwidget_text, my app gets opened instead of the dialog box.当我尝试单击 ID 为 appwidget_text 的视图时,我的应用程序被打开而不是对话框。 Where am I going wrong?我哪里错了?

Hej Pro, Hej Pro,

a PendingIntent cannot target a DialogFragment directly - only Activity, Service or Broadcast. PendingIntent不能直接针对DialogFragment - 只能是 Activity、Service 或 Broadcast。

You have to set an action on your Intent to identify the click on the view with id ( R.id.appwidget_text ).您必须在 Intent 上设置一个操作,以识别对带有 id ( R.id.appwidget_text ) 的视图的点击。 When your app is opened and check the received Intent if it is the action for displaying your DialogFragment and navigate to it.当您的应用程序打开并检查收到的 Intent 是否是显示您的DialogFragment并导航到它的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM