在Android的可点击的小部件 [英] Clickable widgets in android

查看:94
本文介绍了在Android的可点击的小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发者文档已经似乎已经在这里让我失望。我可以创建一个静态控件想也没想,我甚至可以然而创造这样的模拟时钟小工具,将更新自己的窗口小部件,我不能为我的生活弄清楚如何创建一个小部件的反应,当用户点击到它。这里是最好的code样品的开发者文档,给予什么是小部件的活动应包含(唯一的其他提示作为API的演示,只创建一个静态控件):

The developer documentation has seemed to have failed me here. I can create a static widget without thinking, I can even create a widget like the analogue clock widget that will update itself, however, I can not for the life of me figure out how to create a widget that reacts to when a user clicks on it. Here is the best code sample that the developer documentation gives to what a widget activity should contain (the only other hint being the API demos, which only creates a static widget):

public class ExampleAppWidgetProvider extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current App Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

来自: Android的开发者文档的窗口小部件页

所以,它看起来像挂起的意图部件被点击时被调用,它是基于关闭的意图(我不是很确定的意图和挂起的意图之间的区别是什么),而其目的是为在为ExampleActivity类。所以我做了我的样本活动类的简单的活动,创建时,将创建一个MediaPlayer对象,并启动它(它不会永远释放的对象,所以它最终会崩溃,这里是它的code:

So, it looks like pending intent is called when the widget is clicked, which is based off of an intent (I'm not quite sure what the difference between an intent and a pending intent is), and the intent is for the ExampleActivity class. So I made my sample activity class a simple activity that when created, would create a mediaplayer object, and start it (it wouldn't ever release the object, so it would eventually crash, here is it's code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
    mp.start();
}

然而,当我添加窗口小部件到主屏幕,并点击它,没有什么发挥,其实,当我设置更新计时器只是几百毫秒(在appwidget提供商XML文件)没有出场。此外,我设置断点,发现不仅是它从来没有达到的活动,但没有破发点,我将永远会被触发。 (我还没有想通了,这是为什么),然而,logcat的似乎表明,该活动类文件正在运行。

However, when I added the widget to the home screen, and clicked on it, nothing played, in fact, nothing played when I set the update timer to just a few hundred milliseconds (in the appwidget provider xml file). Furthermore, I set break points and found out that not only was it never reaching the activity, but no break points I set would ever get triggered. (I still haven't figured out why that is), however, logcat seemed to indicate that the activity class file was being run.

那么,有没有什么我可以做的就是一个appwidget向点击应对?由于onClickPendingIntent()方法是最接近我发现到的onClick类型的方法。

So, is there anything I can do to get an appwidget to respond to a click? As the onClickPendingIntent() method is the closest I have found to a onClick type of method.

非常感谢你。

推荐答案

首先,添加具有恒定的静态变量。

First, add a static variable with a constant.

public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";

然后你需要的操作添加到你的意图添加意图挂起的意图之前:

Then you need to add the action to the intent before you add the intent to the pending intent:

Intent intent = new Intent(context, widget.class);
intent.setAction(YOUR_AWESOME_ACTION);

(凡widget.class是类的AppWidgetProvider的,当前类)

(Where widget.class is the class of your AppWidgetProvider, your current class)

然后,您需要创建一个PendingIntent与getBroadcast

You then need to create a PendingIntent with getBroadcast

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

在小部件设置onClickPendingIntent为可点击查看

Set the onClickPendingIntent for the clickable view in your widget

remoteView.setOnClickPendingIntent(R.id.widgetFrameLayout, pendingIntent);

接下来,重写的onReceive方法在同一类:

Next, override the onReceive method in the same class:

@Override
public void onReceive(Context context, Intent intent) {
 super.onReceive(context, intent);

然后再通过查询意图响应您的按键presses返回的的onReceive方法中你的行动:

And then respond to your button presses by querying the intent returned for your action within the onReceive method:

if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {
   //do some really cool stuff here
}

和应该这样做!

这篇关于在Android的可点击的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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