我在我的应用程序中托管小部件,需要知道用户何时单击小部件,或小部件配置意图何时开始(对于具有配置的小部件)
OnUserLeaveHint 不是一个选项。
我已经实现了您想要实现的目标。解决方案的想法是使用 is_clicked=true 的 bundle 参数将 OnClick 挂起意图设置为小部件。
这是你可以做的:
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
活动被打开。在此活动中进行下一个编码:
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
}
...
...
}
注意::
希望,我可以帮助你。
用一些布局包装你的小部件并覆盖 onInterceptTouchEvent 方法。