gpt4 book ai didi

android - 识别点击可配置的小部件

转载 作者:太空狗 更新时间:2023-10-29 12:49:44 25 4
gpt4 key购买 nike

我在我的应用程序中托管小部件,需要知道用户何时单击了小部件,或者小部件配置 Intent 何时开始(对于具有配置的小部件)

OnUserLeaveHint 不是一个选项。

最佳答案

我已经完全实现了您想要实现的目标。解决方案的想法是使用 is_clicked=true 的 bundle 参数将 OnClick pending intent 设置到 widget。

这是你可以做的:

1.在使用 RemoteViews 设置小部件布局的同一位置,执行下一步:

/*
* Create pending intent to configuration activity
*/
Intent intent = new Intent(context, ConfigurationMainActivity.class);

/*
* Add values with this intent: widget id, and is_clicked = true
*/
Bundle extra = new Bundle();
extra.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
extra.putBoolean(ConfigurationMainActivity.IS_ON_WIDGET_CLICK_KEY, true);
intent.putExtras(extra);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

/*
* Set this intent to one of the views in the widget
*/
remoteViews.setOnClickPendingIntent(R.id.widget_main, pendingIntent);

2.当用户单击小部件时,ConfigurationMainActivity Activity 将打开。在此 Activity 中执行下一个编码:

public static final String IS_ON_WIDGET_CLICK_KEY = "IS_ON_WIDGET_CLICK_KEY";

@Override
protected void onCreate(Bundle savedInstanceState)
{
// some usual stuff
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuration);

Intent intent = getIntent();
Bundle extras = intent.getExtras();

// get the widget id that was transferred from on click event
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

// ---> here is what you asking for --->
// check whereas the widget was clicked by the user or not
boolean isOnWidgetClick = extras.getBoolean(IS_ON_WIDGET_CLICK_KEY, false);

if (isOnWidgetClick)
{
// ----- Do here whatever you want ------
}
else
{
// the code of first time widget configuration
}

...
...
}

注意::

  • IS_ON_WIDGET_CLICK_KEY - 只是一个在多个类中使用的常量。您可以在远程 View 设置和此处查看它

希望能帮到你。

关于android - 识别点击可配置的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com