San*_*ndy 1 android widget appwidgetprovider android-appwidget
我正在尝试一些非常简单的代码来获取Android小部件,但没有运气.我环顾四周,没有找到一个好的答案.
我想要的(现在)是在触摸小部件时递增计数器并显示当前值.
这是我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的清单的一部分:
<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>
Run Code Online (Sandbox Code Playgroud)
日志显示onReceive()在被放置在主屏幕后立即被调用,并且在被触摸之后,数字永远不会增加.我不完全理解小部件是如何工作的,但是它们在onUpdate()之后被杀死了吗?所以要做到这一点,我将不得不使用某种持久存储?
此外,如果我当前添加另一个小部件,即使我只触摸一个小部件,它们也会显示相同的值并递增.有没有办法让每个和任何小部件拥有它自己的计数器?
谢谢!
ChD*_*ers 11
其实你已经回答了你的问题.但是让我们澄清一些事情:
答:AppWidgets只要在主屏幕上就不会被杀死.但他们不属于你.它们在家庭应用程序的运行过程中运行.更具体地说,他们的视图在主应用程序的进程中运行,这就是为什么你看到你的小部件,但它没有做你所期望的,这就是为什么我们使用RemoteViews而不是Views来更新它们.
B.另一方面,一旦onReceive方法完成,AppWidgetProviders(在你的情况下是WordWidget类)就会被销毁.每次调用onReceive方法时,都会重新初始化它们中的所有变量.这就是为什么你的号码永远不会增加 AppWidgetProvider的目的是更新小部件的RemoteView并通知您的应用程序已注册的广播已到达.
C. AppWidgetProvider的onUpdate方法为您提供了一个包含必须更新的小部件ID的数组.这是唯一可用于获取窗口小部件实例及其ID的数量的代码点.由于RemoteViews无法从窗口小部件的视图中获取一些有用的值(例如,您无法从TextView中读取计数器值),因此您必须使用提供的信息并且DO会持久保存每个窗口小部件ID的计数器值.当调用下一个onUpdate时,您将从存储中读取值,增加它,使用新值更新窗口小部件,然后将新值存储回来.
D.如果你的小部件在更新自己的时候必须做很多事情或减慢事情(比如网络),你应该考虑使用服务.但是在你的情况下(增加一个数字),只要你将计数器持久存储在持久存储或其他地方,你的方法就可以了.
最后我注意到,在你的onReceive覆盖中,只有在你没有收到"UPDATE_NUMBER"动作时才调用"super.onReceive".这不是一个好习惯,除非有一个好的理由(这是另一个故事)总是将super.onReceive称为覆盖中的第一个或最后一个命令.
希望这可以帮助...
| 归档时间: |
|
| 查看次数: |
2988 次 |
| 最近记录: |