android - 将数据从小部件传递到应用程序

标签 android android-widget android-pendingintent

我正在为我的应用程序开发一个小部件,我想将特定数据从小部件传递到应用程序。用户可以添加多个版本的小部件,我需要检测它来自哪个小部件,以便我可以在应用程序中显示特定信息。

到目前为止我所做的工作,但前提是我完全退出我的应用程序(反复按下后退按钮),否则它似乎没有检测到传入的数据。

这是我必须将数据从小部件传递到应用程序的代码:

public class WidgetProvider extends AppWidgetProvider  {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        final int N = appWidgetIds.length;

        for (int i=0; i<N; i++) 
            UpdateWidget(context, appWidgetManager, appWidgetIds[i]);
    }

    public static void UpdateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
    {
        Intent intent = new Intent(context, Main.class);
        final Bundle bun = new Bundle();
        bun.putInt("widgetId", appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtras(bun);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setOnClickPendingIntent(R.id.widgetText, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

更新

多亏了 Tal Kanel,我们才接近完成这项工作。我的应用程序中有一个 ActionBar 并遵循 Google 的指南,我在 onOptionsItemSelected 中有此代码:

if (item.getItemId() == android.R.id.home) {
          final Intent intent = new Intent(activity, Main.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);
          return true;
    } 

如果我单击 ActionBar 中的“后退”按钮,然后最小化我的应用程序并单击小部件,此代码始终为空:

    if (getIntent().getExtras() != null)
    {

    }

更新 2

实际上,只要返回到我的应用程序中的主要 Activity ,就会导致 getExtras() 为空。我的应用程序小部件中有这段代码,它不会向应用程序传递任何额外内容:

    PendingIntent piMain = PendingIntent.getActivity(context, appWidgetId, new Intent(context, Main.class), PendingIntent.FLAG_UPDATE_CURRENT);

    rv.setOnClickPendingIntent(R.id.widgetTitle, piMain);

如果我在我的小部件中单击该部分,然后返回并单击我的小部件中应该传递额外内容的部分,getExtras() 始终为 null。

最佳答案

首先 - 当您按返回键直到返回主屏幕时,您的应用程序并没有真正关闭。只是在此阶段不存在来自您的应用程序的任何 Activity 。
我认为了解这一点非常重要,以防万一您没有...

关于你的问题:
您正在尝试使用 FLAG_ACTIVITY_CLEAR_TOP 从小部件启动 Activity ,它定义为:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

这意味着如果您的 Main Activity 已经在前台运行,那么当您从小部件启动 Activity 时 - 此 Main Activity 之上的所有其他 Activity 将从堆栈中删除,并且 Main 将恢复并进入 Activity.onNewIntent(intent) 回调。

这就是您看不到数据的原因 - 因为 Activity 不会以新 Intent 重新启动,而只会以您发送的新 Intent 进入 onNewIntent(intent)。

我敢肯定,如果您在 onNewIntent(intent) 方法中放置一个断点 - 您会看到您发送的 intent 已传送到那里,以及所有额外的数据。

您的代码的另一个问题:检查 Intent.putExtras(boundle) 方法的定义:

Add a set of extended data to the intent. The keys must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

你好像没有按照规则调用你的键前缀。这就是它为空的原因。

希望对你有帮助。

关于android - 将数据从小部件传递到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377003/

相关文章:

java - 在android中绘制3d图形的方法是什么?

android - 在不验证银行账户的情况下发布应用程序

android - 如何在 android 中重复特定星期几的闹钟

android - 具有大量 View 或动态 View 的 Viewpager

android - Android 中的 URL 错误

java - 定位 S+(版本 31 及更高版本)需要指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

android - 获取有关 PendingIntent 的 Intent 的详细信息

Android Geofencing - 没有 future 的 Intent ?

java - 让 GsonConverterFactory 解析某些字段?

android - AmazingListView 适配器未下载?