Android小工具ID只对当前控件 [英] android widget id only for current widget

查看:249
本文介绍了Android小工具ID只对当前控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以得到我当前在的onUpdate为widgetid() AppWidgetProvider 的方法是什么?

Can I get my current widgetId inside onUpdate() method of AppWidgetProvider?

我发现很多帖子关于意向书场发送 appWidgetId ,但我不明白的地方我必须在发送之前得到它。

I found many posts about sending appWidgetId in field of Intent, but I can't understand where I have to get it before sending.

我没有找到获取当前唯一部件ID,而无需配置活动的方法。

I didn't find the methods for getting only current widget Id without configuration activities.

推荐答案

尝试的onUpdate

try in onUpdate

    int a = appWidgetIds.length; 

如果返回1,则:

If this returns 1, then:

    int yourAppWidgetId = appWidgetIds[0];

应该返回您appWidgetId。

should return your appWidgetId.

没有看到任何code汇入作业很难给你正确的答案,但我会给你一个例子。如果你创建了RemoteViews你的小工具,你必须创建为每个远程视窗的意图。在这个意图,您可以设置一个ID,例如:

without seeing any code it´s difficult to give You the right answer, but I will give You an example. If you created your RemoteViews for your widgets, you have to create an intent for every RemoteView. At this intent, You could set an ID, for example:

    RemoteViews remoteViews = new RemoteViews(getApplicationContext()
                .getPackageName(), 
                    layout.your_widget_layout);

        Intent intent1 = new Intent(getApplicationContext(),
                YourAppWidgetProvider.class);

    intent1.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,widgetIds);
        intent1.putExtra("ID", "one");

然后创建一个的PendingIntent,你可以设置为remoteViews:

Then create a PendingIntent, which You can set to remoteViews:

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 1, intent1,
                PendingIntent.FLAG_ONE_SHOT);

    remoteViews.setOnClickPendingIntent(R.id.your_widget_button_id_in_xml,
                pendingIntent);

如果用户pressed小部件,你可以进去的onReceive()ID为

If user pressed the widget, you can get id inside onReceive() with

    String a = intent.getStringExtra("ID");

但这仅仅是一个不完整的例子,有很多事情要做。我认为这是一个很好的方式,如果你阅读本教程:

But this is only an incomplete example, there is a lot more to do. I think it is a good way, if you read this tutorial:

http://www.android10.org/index.php/articlesgeneralprogramming/315-app-widgets-tutorial

有是带有updateService一个很好的例子。

There is a good example with an updateService.

这篇关于Android小工具ID只对当前控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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