在主屏幕小部件中遇到变量问题 [英] Having trouble with variables in home screen widget

查看:95
本文介绍了在主屏幕小部件中遇到变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我为Android 2.2创建了一个简单的主屏幕小部件.它仅包含一个按钮和一个背景图像.我想做的是,当用户单击按钮时,我会显示一个带有数字的Toast味精.每次单击按钮时,此数字将从零开始增加.

因此,香港专业教育学院宣布我的变量如下:

Hello

I''ve created a simple home screen widget for the Android 2.2. It comprises of a button and a background image only. What im trying to do is that when user clicks on the button, i show a Toast msg with a number. This number is to be increased every time button is clicked, starting from zero.

So ive declared my variable as follows:

public class MainWidget extends AppWidgetProvider {

   private static int myNum = 0;

...



在onReceive事件中,我执行此操作(在检查了单击按钮之后):



And in the onReceive event I do this (after checking the button was clicked):

myNum += 1;
Toast.makeText(context, "myNum:" + myNum, Toast.LENGTH_LONG).show();



这很好.每当用户单击按钮时,数字都会增加1并显示在屏幕上.我希望这个数字是特定于实例的.如果我从主屏幕上删除该小部件并再次添加回它,则编号将从上次的位置继续.例如,如果number当前为4.我删除该小部件并添加并单击按钮,它将显示5.我希望它重新启动
从1.

我尝试了各种声明:



This works fine. Whenever user clicks the button the number is increased by 1 and displayed on screen. I want this number to be instance-specific. If i remove the widget from the home screen and add it back again, the numbering continues from the point where it was last time. For example if number is currently 4. I remove the widget and add back and click button, it will display 5. I want it to restart
from 1.

I''ve tried various declarations:

private int myNum = 0;
int myNum = 0;
static int myNum = 0;



但是没有任何作用.如果我删除"static"关键字,则该数字根本不会增加.每次单击该按钮时,它将一直显示1.我希望该数字是特定于实例的,即无论何时删除和添加小部件,都应从1重新开始.同样,如果用户在其主屏幕上放置了多个小部件,则两个小部件的编号应彼此独立.我该如何实现?看起来像一个简单的任务,但完全困惑如何声明我的整数.请任何人帮助.

这是完整的代码:单击此处查看 [ Gmail文本云小部件 [



But nothing work. If I remove the "static" keyword then the number doesn''t increase at all. Every time the button is clicked, it keeps displaying 1. I want the number to be instance-specific i.e. to be restarted from 1 whenever the widget is removed and added back. Also if the user places more than one widgets on his home screen then both the widgets'' numbering should be independent from each other. How do I achieve this? Seems like a simple task but totally confused how to declare my integer. Please anyone help.

Here is the complete code: click here to view[^]

-Faraz Azhar

@Nagy: No thats not the issue. I''ve put Toasts msgs on every event.. OnDeleted, OnDisabled, etc. So I know all these events are firing properly.

Have a look at this link below. Another person is also having the exact same problem. Maybe you can understand better in his wording:
Gmail Text Cloud Widget[^]

推荐答案

当您从主屏幕上删除小部件图标时,它并没有结束;这是正常的行为.您实际上并没有关闭应用程序,只需删除图标即可.如果您拥有高级任务管理器之类的工具,您将能够看到它仍在运行.

可能值得检查OnDeleted事件是否正在触发.
When you remove the widget icon from the home screen it is not ending; this is normal behaviour. You are not actually closing the app, you are simple removing the icon. If you get a tool like Advanced Task Manager, you will eb able to see that it is still running.

It might be worth checking if the OnDeleted event is firing.


这篇关于在主屏幕小部件中遇到变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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