我正在我的应用程序中托管小部件,并且需要知道用户何时单击小部件,或者何时启动小部件配置意图(对于具有配置的小部件)
OnUserLeaveHint不是一个选项。
答案 0 :(得分:2)
我已经完全实现了你想要实现的目标。该解决方案的想法是使用bundle参数is_clicked = true将OnClick挂起意图设置到窗口小部件。
这是你可以做的:
<强> 1 强> 在使用RemoteView设置窗口小部件布局的相同位置,执行下一步:
/*
* 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
}
...
...
}
注意:强>:
希望,我可以帮助你。
答案 1 :(得分:1)
用一些布局包装你的小部件并覆盖onInterceptTouchEvent方法。