如何将对象传递给窗口小部件

时间:2017-11-17 21:29:09

标签: java android android-studio widget home-screen-widget

我目前正在制作一个包含小部件的应用。 放置应用程序时,会打开一个WidgetConfigure活动,您可以在其中从recycleViewer中选择一个对象。

对象(Kitten)实现 parcelable ,recyleviewer由适配器类填充。

当触发该recyleviewer的eventListner时,必须将所选对象传递给窗口小部件,因为我需要在我的窗口小部件中使用该对象的属性。

我现在完全陷入困境,并且无法将对象传递给窗口小部件。 如何将对象传递给窗口小部件?

这是我将在eventListner中运行的代码 LongEventConfigureActivity

private void makeWidget(Event myObj){

    // How I normally passing data to an activity/fragment

    //Intent i = new Intent();
    //Bundle b = new Bundle();
    //b.putParcelable(CONST_PARCELKEY, myObj);
    //i.putExtras(b);
    //i.setClass(this, ObjectDetailActivity.class);
    //startActivity(i);

    // Example of how to pass data to the widget
    final Context context = LongEventWidgetConfigureActivity.this;

    // When the button is clicked, store the string locally
    String widgetText = txtWidgetText.getText().toString();
    saveTitlePref(context, mAppWidgetId, widgetText);

    // It is the responsibility of the configuration activity to update the app widget
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    LongEventWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);

    // Make sure we pass back the original appWidgetId
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
    finish();

}


@Override
public void onItemClick(Event e) {
    makeWidget(e);
}

此外,我需要找到一种方法,当用户点击此窗口小部件时,我的应用程序的某个活动将打开,将接收该对象。

Summery of the problem (I don't have the privilege to post an image yet)

0 个答案:

没有答案