【问题标题】:Android - Communications between a widget and its appAndroid - 小部件与其应用程序之间的通信
【发布时间】:2011-11-18 00:03:05
【问题描述】:

我有一个小部件,它显示下面带有文本的各种图像,并且在应用程序本身中设置了相同的 UI。我希望小部件不仅能够打开应用程序,而且能够根据小部件中显示的图片打开应用程序,然后在应用程序中显示相同的图像。但是,我很难让它发挥作用。

谢谢。

【问题讨论】:

  • 你到底尝试了什么,什么没用?也请发布一些相关的源sn-ps。
  • 你可以使用Intents和Bundles

标签: android android-widget communication android-pendingintent


【解决方案1】:

假设 ui 中有几张图片,你可以为每张图片设置不同的 Intent,而这些 Intent 各自将针对不同的 Activity。

这篇文章:http://rxwen.blogspot.com/2012/10/communication-between-android-widget.html 可能会给你一些提示。

【讨论】:

  • 我已经有一段时间没有做这个了,但我相信你的建议就是我们所做的。但是,我们必须将一个随机数传递给 getActivity() 的 requestCode 参数。否则,Android 会在 remoteViews 中重用挂起的 Intent,即使它们都已明确分配了自己的 PendingIntent 对象。
【解决方案2】:

将此代码添加到 widget.java 文件中以启动您的应用程序。而不是MainActivity.class,您可以调用任何活动

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

        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);
        Intent openApp = new Intent(context,MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);

        views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);
        appWidgetManager.updateAppWidget(appWidgetId,views);
}

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 2020-11-14
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多