从窗口小部件的数据传递到App [英] Passing data from widget to app

查看:125
本文介绍了从窗口小部件的数据传递到App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展我的应用程序的窗口小部件,我想从部件到应用程序传递特定的数据。用户可以添加小部件的多个版本,我需要检测哪个窗口小部件是从这样我就可以显示应用程序的特定信息来了。

I'm developing a widget for my app and I'd like to pass specific data from the widget to the app. The user can add multiple versions of the widget and I need to detect which widget it is coming from so I can show specific information in the app.

我有什么工作,到目前为止,但只有当我完全退出我的应用程序(反复pressing后退按钮),否则它似乎并没有被检测传递的数据。

What I have works so far, but only if I exit my app completely (repeatedly pressing the back button), otherwise it doesn't seem to be detecting the data being passed in.

这在code我必须从部件到应用程序传递数据:

This the code I have to pass the data from the widget to the app:

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);
    }
}

更新

越来越接近得到这个工作,感谢塔尔Kanel。我在我的应用程序有一个动作条并按照谷歌的指引,我在 onOptionsItemSelected 这code:

Getting close to getting this working, thanks to Tal Kanel. I have an ActionBar in my app and following Google's guidelines, I have this code in 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;
    } 

如果我点击了动作条,然后最小化我的应用程序,并单击插件的后退按钮,这code总是空:

If I click on the "back" button in the ActionBar, then minimize my app and click on the widget this code is always null:

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

    }

更新2

其实,刚要回的主要活动在我的应用程序的原因 getExtras()为空。我在我的应用程序插件此code,不传递任何额外的应用程序:

Actually, just going back to the main activity in my app causes getExtras() to be null. I have this code in my app widget, that does not pass any extras to the app:

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

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

如果我点击我的窗口小部件的一部分,然后回去,然后按一下是应该通过临时演员我的小部件的一部分, getExtras()始终空。

If I click on that part in my widget, then go back and click on the part of my widget that is supposed to pass the extras, getExtras() is always null.

推荐答案

首先 - 您的应用程序不是很接近,当你pressing回直到返回到主屏幕。它只是没有从应用程序的活动存在于这个阶段。结果
我想知道,这是非常重要的,如果你没有......

first of all - your application not really close when you pressing back until returning to home screen. it's just that no activities from your application exists in this stage.
I think it's very important to know that, in case you didn't...

你的问题:结果
您尝试从与FLAG_ACTIVITY_CLEAR_TOP小部件发起的活动,它的定义:

about your problem:
you are trying to launch activity from the widget with the FLAG_ACTIVITY_CLEAR_TOP, that it definition:

如果设置,并且正在启动的活动已经在当前任务运行,则代替开展这一活动的新实例,都在它之上的其他活动将被关闭,这个意图将交付给(现在的顶部),老年活动作为一个新的意图。

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.

这意味着,如果你的主要活动已在forground运行,那么当你开始从插件的活动 - 在这个主要活动的顶部所有其他活动将会从栈中删除,并主要将恢复,并进入到<强> Activity.onNewIntent(意向)回调。

that means that if your Main activity already running in forground, then when you start the activity from the widget - all other activities on top of this Main activity will be removed from stack, and Main will resume and get into to Activity.onNewIntent(intent) callback.

这就是为什么你看不到数据 - 因为活动不与新的意图重新启动,但只有你发送的新意图进入onNewIntent(意向)

that's why you don't see the data - because the activity don't restart with the new intent, but only get into the onNewIntent(intent) with the new intent you sent.

我敢肯定,如果你会把一个破发点中的onNewIntent(意向)方法 - 你会看到你发送的意图有交付,有额外的所有数据

I'm sure that if you'll put a break point in the onNewIntent(intent) method - you'll see that the intent you sent is delivered there, with all the extra's data.

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

another problem with your code: check the definition of the Intent.putExtras(boundle) method:

添加了一组扩展的数据来的意图。密钥必须包括一个包preFIX,例如应用程序com.android.contacts将使用的名称,如com.android.contacts.ShowAll。

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".

好像你不按照规则拨打你的关键preFIX。这就是为什么它是空。

it seems like you don't call your key prefix according to the rules. that's why it's null.

希望它帮助你。

这篇关于从窗口小部件的数据传递到App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆