在Android中,如何在同一窗口小部件的多个实例加以区分? [英] In Android, how to distinguish between multiple instances of the same widget?
问题描述
我正在做一个小部件。它将被点击时的随机数字。当我加入这个小工具的主屏幕上的多,他们开始在同一时间点击时产生的随机数。我要的是点击一个窗口小部件时,只更新自己和周围的人非。请帮帮我。
我的code:类UpdateWidgetService
@覆盖
公共无效调用onStart(意向意图,诠释startId){
//创建一些随机数据
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
INT [] = allWidgetIds intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
对于(INT为widgetid:allWidgetIds){
//创建一些随机数据
INT数=(新随机()nextInt(100)。); RemoteViews remoteViews =新的RemoteViews(this.getApplicationContext()getPackageName(),R.layout.widget_layout。);
Log.w(WidgetExample,将String.valueOf(数));
//设置文本
remoteViews.setTextViewText(R.id.update,随机+将String.valueOf(数)); //注册一个onClickListener
意图clickIntent =新意图(this.getApplicationContext(),MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,为widgetid); 的PendingIntent的PendingIntent = PendingIntent.getBroadcast(getApplicationContext(),为widgetid,clickIntent,0);
remoteViews.setOnClickPendingIntent(R.id.btn_update,的PendingIntent);
appWidgetManager.updateAppWidget(为widgetid,remoteViews);
}
stopSelf();
super.onStart(意向,startId);
}
类MyWidgetProvider:
@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
Log.w(LOG所谓的onUpdate法);
//获取所有ID
组件名thisWidget =新的组件名(背景下,MyWidgetProvider.class);
INT [] = allWidgetIds appWidgetManager.getAppWidgetIds(thisWidget); //构建意图调用服务
意向意图=新意图(context.getApplicationContext(),UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds); //更新通过服务窗口小部件
context.startService(意向);
}
小部件的每一个都由特定的ID标识。你的 UpdateWidgetService
类看起来是正确的循环在ID的。
问题是在你的 MyWidgetProvider.onUpdate()
方法。这code是错误的:
//获取所有ID
组件名thisWidget =新的组件名(背景下,MyWidgetProvider.class);
INT [] = allWidgetIds appWidgetManager.getAppWidgetIds(thisWidget);
注释解释了为什么这是错的 - 它得到的所有小部件的ID
看看你的onUpdate()
方法的参数:
-
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds)
在这里,你可以看到小部件的阵列的ID是给你,你必须将此数组传递给您的更新服务:
-
INT [] appWidgetIds
所以你的code应该结束了寻找这样的:
@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
Log.w(LOG所谓的onUpdate法); //构建意图调用服务
意向意图=新意图(context.getApplicationContext(),UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds); //更新通过服务窗口小部件
context.startService(意向);
}
这样,你只更新部件,系统发送给您,而不是每次都更新所有的人。
I'm making a widget. It will random a number when clicked. When I add multiple of this widget on home screen, they start to generate random number at the same time when clicked. What I want is when a widget is clicked it only updates itself and non of the others around it. Please, help me. My code: class UpdateWidgetService
@Override
public void onStart(Intent intent, int startId) {
// create some random data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
for (int widgetId : allWidgetIds) {
// create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, "Random: " + String.valueOf(number));
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), widgetId, clickIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.btn_update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
class MyWidgetProvider:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w(LOG, "onUpdate method called");
// Get all ids
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
// Update the widgets via the service
context.startService(intent);
}
Each of your widgets is identified by a specific ID. Your UpdateWidgetService
class looks like it is correctly looping over the ID's.
The issue is in your MyWidgetProvider.onUpdate()
method. This code is wrong:
// Get all ids
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
The comment explains why it is wrong - it gets all the widget ID's.
Look at the parameters in your onUpdate()
method:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
Here you can see an array of widget ID's is given to you, and you must pass this array to your update service:
int[] appWidgetIds
So your code should end up looking something like this:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w(LOG, "onUpdate method called");
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
// Update the widgets via the service
context.startService(intent);
}
That way you are only updating the widgets that the system sends to you, instead of updating all of them each time.
这篇关于在Android中,如何在同一窗口小部件的多个实例加以区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!