帮助使用appwidget与广播接收器和服务? [英] help for using appwidget with broadcastreceiver and service?

查看:173
本文介绍了帮助使用appwidget与广播接收器和服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小窗口,其中有两个按钮和一个服务类中,我对按钮的clikcing两种方法我想打电话给在服务类中的方法。
任何参考或code的例子吗?
我该怎么办呢?

i have one widget in which there are two buttons and one service class in which i have two methods on clikcing of button i want to call the methods in service class. any reference or code example? how can i do it?

thankx

推荐答案

我猜你的问题涉及到appWidgets。如果没有,下面的答案很可能是用处不大,但你仍然可以适应它的一部分,以自己使用,我想。

I'm guessing that your question pertains to appWidgets. If not, the answer below will probably be less useful, although you can still adapt part of it to your own use, I think.

首先声明一些行动上,是这样的:

First declare some action(s), like this:

    public static final String ACTION_HIDE_GUIDE_OVERLAY = "dk.something.appwidget.calendar.guide.hide";

使用的动作(S)宣布一个Intent和的PendingIntent:

Use the action(s) to declare an Intent and a PendingIntent:

    Intent intent = new Intent(context, CalendarService.class);
    intent.setAction(CalendarService.ACTION_HIDE_GUIDE_OVERLAY);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent buttonPendingIntent1 = PendingIntent.getService(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

然后用远程视窗为您的小部件注册的PendingIntent:

Then register the pendingIntent with the RemoteView for your widget:

    views.setOnClickPendingIntent(R.id.calendar_button_1, buttonPendingIntent1);

最后,在你的服务的onStartCommand或onHandleIntent,检查意向的行动,以确定要调用其中你的方法:

Finally, in the onStartCommand or onHandleIntent of your service, examine the action of the intent to determine which of your methods to call:

  if (intent.getAction().equals(CalendarService.ACTION_INITIALIZE_CALENDAR)) {
        int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        onInitializeCalendar(appWidgetIds);
} else if (intent.getAction().equals(CalendarService.ACTION_HIDE_GUIDE_OVERLAY)) {
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        onHideGuideOverlay(appWidgetId);
} else ...

注:code段采取几乎是直接从我的玻璃窗口小部件,从而使其适应自己使用,你就必须用自己的两个动作来替代作用常数(一个动作每一个按钮),与你的其中一个按钮的实际ID替换R.id.calendar_button_1,用自己的服务类的名称和方法onInitializeCalendar和onHideGuideOverlay用两种方法在你的服务类要调用的名称替换CalendarService。

Note: The code snippets are taken almost directly from my Glass Widgets, so to adapt it to your own use, you would have to replace the action constant with your own two actions (one action for each button), replace R.id.calendar_button_1 with the actual id of one of your buttons, replace the CalendarService with the name of your own service class and the methods onInitializeCalendar and onHideGuideOverlay with the name of the two methods in your service class that you want to call.

如果你只曾经有你的部件可见的一个实例在同一时间,那么你就需要反复折腾appWidgetId,但你可能无法保证。我有一些设置,可以小部件实例变化到窗口小部件实例,因此我把那个被点击进入的意图,这里的服务可以在它我的服务请求时,得到appwidget的id。

If you only ever have one instance of your widget visible at one time, then you don't need to fiddle around with the appWidgetId, but you probably can't guarantee that. I have some settings that can vary from widget instance to widget instance, so I put the id of the appwidget that was clicked into the intent, where the service can get at it when servicing my request.

这篇关于帮助使用appwidget与广播接收器和服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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