Android小工具不响应触摸 [英] Android widget not responding to touches

查看:210
本文介绍了Android小工具不响应触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只是一些很简单的code得到一个Android小工具去,但没有运气。我到处看了看四周,没有找到一个很好的答案。

I'm trying just some very simple code to get an Android widget going but with no luck. I've looked around everywhere and haven't found a good answer.

所有我想要的(现在)是递增计数器,当小部件被触摸和显示的当前值。

All I want (for now) is to increment a counter when the widget is touched and display the current value.

这是我的AppWidgetProvider:

This is my AppWidgetProvider:

public class WordWidget extends AppWidgetProvider
{
    Integer touchCounter = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        //This is run when a new widget is added or when the update period expires.
        Log.v("wotd",  "Updating " + appWidgetIds.length + " widgets");

        for(int x = 0; x < appWidgetIds.length; x++)
        {
            Integer thisWidgetId = appWidgetIds[x];

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            remoteViews.setTextViewText(R.id.mainText, touchCounter.toString());

            Intent widgetIntent = new Intent(context, WordWidget.class);
            widgetIntent.setAction("UPDATE_NUMBER");

            PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);

            remoteViews.setOnClickPendingIntent(R.id.widgetLinearLayout, widgetPendingIntent);
            appWidgetManager.updateAppWidget(thisWidgetId, remoteViews);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.v("wotd", "In onReceive with intent=" + intent.toString());
        if (intent.getAction().equals("UPDATE_NUMBER"))
        {
            Log.v("wotd", "In UPDATE_NUMBER");
            touchCounter++;
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            remoteViews.setTextViewText(R.id.mainText, touchCounter.toString());
        } else
        {
            Log.v("wotd", "In ELSE... going on to super.onReceive()");
            super.onReceive(context, intent);
        }
    }
}

这是我表现的一部分:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <receiver
        android:icon="@drawable/ic_launcher"
        android:name="com.example.mywidget.WordWidget"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="UPDATE_NUMBER" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widgetinfo" />
    </receiver>

</application>

日志显示的onReceive()被放置在主屏幕上后,立即调用,并且被感动后,但数量不增加。我不完全理解小部件是如何工作的,但他们的onUpdate后处死()?因此,要做到这一点,我将不得不使用某种持续性存储的?

The log shows the onReceive() is called immediately after being placed on the home screen, and after being touched, however the number never increases. I don't totally understand how widgets work, but are they killed after onUpdate()? So to do this I would have to use some kind of persistant storage?

另外,如果我现在再添两个小部件会表现出相同的价值观和增量,即使我只是碰之一。有没有为每个方法和任何小部件有它自己的柜台?

Also, if I currently add another widget both would show the same values and increment even if I just touch one. Is there a way for each and any widget to have it's own counter?

谢谢!

推荐答案

其实你已经回答了你的问题。但是,让我们澄清一些事情:

Actually you have answered your question. But let's clarify some things:

一个。 AppWidgets都没有,只要他们是一个主屏幕上丧生。但他们不属于你。它们在家庭应用的进程中运行。更具体地讲他们的意见在家庭应用的过程中运行,这就是为什么你看到你的小部件,但它不会做你期望什么,这就是为什么我们为了更新它们所使用RemoteViews代替意见

A. AppWidgets are NOT killed as long as they are on a home screen. But they don't belong to you. They are running in the process of the home application. To be more specific their views are running in the process of the home application, this is why you are see your widget but it doesn't do what you are expected and this is why we are using RemoteViews instead of Views in order to update them.

乙。 AppWidgetProviders(你的情况WordWidget类),在另一方面,被尽快的onReceive方法完成销毁。在他们所有的变量都重新初始化每次的onReceive方法被调用的时间。这就是为什么你的号码永远不会增加。一个AppWidgetProvider的目的是更新widget的RemoteViews并告知您的应用程序注册的广播已经到来。

B. AppWidgetProviders (in your case the WordWidget class), on the other hand, are destroyed as soon as the onReceive method finishes. All the variables in them are re-initialized every time the onReceive method gets called. This is why your number never increases. The purpose of an AppWidgetProvider is to update the widget's RemoteViews and to inform your application that a registered broadcast has arrived.

℃。 AppWidgetProvider的方法的onUpdate为您提供的部件标识必须更新的数组。这是唯一的code点,你可以用它来得到你的小部件实例及其ID的数量。因为RemoteViews的有没有办法从你的widget的浏览一些有用的值(例如您无法读取TextView的计数器的值),所以你必须使用所提供的信息和不要坚持每个窗口小部件ID的计数器值。当下的onUpdate被调用你读从存储的价值,增加它,用新的值更新部件,然后存放新值了。

C. AppWidgetProvider's onUpdate method provides you an Array with the widgets Ids that must be updated. This is the only code point you can use to get the number of your widget instances and their Ids. Because of the RemoteViews there is NO way to get some useful value from the Views of your widget (for example you can NOT read the counter value from the TextView) so you must use the provided information and DO persist your counter values per widget id. When the next onUpdate gets called you read the value from the storage, increase it, update the widget with the new value and then store the new value back.

Ð。如果你的部件有做很多事情或慢的东西(如联网),当它的时间来更新自己,你应该考虑使用服务这一点。但是,在你的情况(增加数字)你的做法是蛮好的,只要你坚持下去的计数器中永久存储或其他地方。

D. If your widget has to do many things or slow things (like networking) when its time to update itself, you should consider using a service for this. But in your case (to increase a number) your approach is just fine as long as you persist the counters in the persistent storage or somewhere else.

最后,我已经注意到,在您的onReceive覆盖您呼叫的super.onReceive只有当你没有收到UPDATE_NUMBER行动。这是不是一个好的做法,除非有很好的理由(这是另一回事)随时拨打super.onReceive作为第一或在覆盖的最后一个命令。

Finally I 've noticed that in your onReceive override you are calling the "super.onReceive" only if you don't receive the "UPDATE_NUMBER" action. This is NOT a good practice, unless there is a GOOD reason (that's another story) always call super.onReceive as the first or the last command in your override.

希望这有助于...

这篇关于Android小工具不响应触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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