Multiple Android Widget instances only updating last widget(多个 Android Widget 实例仅更新最后一个小部件)
问题描述
我尝试使用此链接
更新-我发现未决意图的设置器有问题-每次单击图像视图时-意图都会发送最后定义的小部件的外部详细信息-这意味着在预添加的小部件上定义的其他待处理意图被较新的小部件运行
update - I figured out that there is something wrong with the setter of the pending intent- every time I click on the imageview- the intent sends the extre details of the last defined widget- meaning that the other pending intents that where defined on the pre-added widgets were run-over by the newer widgets
我有一个显示用户选择的图片的应用小部件.(许多小部件 - 许多图片)我的问题是:无论我按下屏幕上的哪个小部件 - 只有最后添加的小部件会更新:这是我的代码
I have an app widget that shows a picture chosen by the user.(many widgets- many pictures) my problem is: no matter which widget on screen I press - only the last added widget gets updated: here is my code
我的小部件 xml 提供程序代码
My Widget xml provider Code
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_Test"
android:minHeight="146dip"
android:minWidth="146dip"
android:updatePeriodMillis="0" />
我的清单 xml 代码
My Manifest xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS" >
</uses-permission>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".Activities.WidgetConfig"
android:screenOrientation="portrait" >
<!-- prbbly delete this line -->
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</activity>
<!-- Test Widget -->
<receiver
android:name=".Simple.Widget"
android:label="@string/app_widget_Test" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_Test_provider" />
</receiver>
<service android:name=".Simple.Widget$TestWidgetService" />
</application>
</manifest>
小部件提供者
public class Widget extends AppWidgetProvider
{
public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget";
public static int[] widgets;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
Intent svcIntent = new Intent(context, TESTWidgetService.class);
widgets = appWidgetIds;
context.startService(svcIntent);
}
public static class TESTWidgetService extends Service
{
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
Mngr.getInstance().pushUpdate(remoteView,
getApplicationContext(), Widget.class);
// No more updates so stop the service and free resources
stopSelf();
}
public RemoteViews buildRemoteView(Context context)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_TEST);
if (widgets != null)
{
int length = widgets.length;
for (int i = 0; i < length; i++)
{
Intent configIntent = new Intent(context,
WidgetConfig.class);
configIntent.setAction(Widget.PREFENCES_WIDGET_CONFIGURE);
String number = AppWidgetManager.EXTRA_APPWIDGET_ID
+ "number";
configIntent.putExtra(number, length);
String widgetID = AppWidgetManager.EXTRA_APPWIDGET_ID + i;
int id = widgets[i];
configIntent.putExtra(widgetID, id);
PendingIntent runTESTPendingIntent = PendingIntent
.getActivity(context, 0, configIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.TESTWidgetImage,
runTESTPendingIntent);
Mngr controller = Mngr.getInstance();
controller.updateTESTWidget(context, remoteViews);
}
}
return remoteViews;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
int oldOrientation = this.getResources().getConfiguration().orientation;
if (newConfig.orientation != oldOrientation)
{
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
Mngr.getInstance().pushUpdate(remoteView,
getApplicationContext(), Widget.class);
}
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
}
}
我的配置活动
public class WidgetConfig extends ListActivity // implements
{
private Bundle m_extras;
private ArrayList<Test> m_tests = null;
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
ImageAdapter m_adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelist);
Resources res = getResources();
Mngr mngr = Mngr.getInstance();
mngr.setResources(res);
Context ctx = getApplicationContext();
mngr.setContext(ctx);
getTests();
m_adapter = new ImageAdapter(ctx, R.layout.row, m_tests);
ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
m_adapter.getImageDownloader().setMode(mode);
setListAdapter(m_adapter);
Intent intent = getIntent();
m_extras = intent.getExtras();
}
private void getTests()
{
m_tests = new ArrayList<Test>();
Resources res = getResources();
String[] TestNames = res.getStringArray(R.array.fav_Test_array);
TypedArray imgs = getResources().obtainTypedArray(
R.array.fav_Test_integer);
for (int i = 0; i < TestNames.length; i++)
{
Test o1 = new Test();
String TestName = TestNames[i];
int resID = imgs.getResourceId(i, -1);
o1.setTestName(TestName);
o1.setIMGID(resID);
m_tests.add(o1);
}
imgs.recycle();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
ListAdapter adapt = l.getAdapter();
Object obj = adapt.getItem(position);
if (obj != null)
{
Mngr mngr = Mngr.getInstance();
Context context = getApplicationContext();
String key = context.getString(R.string.TestWidget_string) + "_"
+ AppWidgetManager.EXTRA_APPWIDGET_ID;
Test Test = (Test) obj;
String val = Test.getIMGID().toString();
mngr.putString(context, key, val);
updateWidget();
}
super.onListItemClick(l, v, position, id);
}
private void updateWidget()
{
Context ctx = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
setResult(RESULT_CANCELED);
if (m_extras != null)
{
Intent resultValue = new Intent();
int numberOfWidgets = m_extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID + "number",
AppWidgetManager.INVALID_APPWIDGET_ID);
for (int i = 0; i < numberOfWidgets; i++)
{
String stringID = AppWidgetManager.EXTRA_APPWIDGET_ID + i;
mAppWidgetId = m_extras.getInt(stringID,
AppWidgetManager.INVALID_APPWIDGET_ID);
/*************************************************************/
/*I Don't really know if I am using this piece of code right */
Uri data = Uri.withAppendedPath(Uri.parse("ABCD"
+ "://widget/id/"), String.valueOf(mAppWidgetId));
resultValue.setData(data);
/*************************************************************/
RemoteViews views = new RemoteViews(ctx.getPackageName(),
R.layout.widget_Test);
Mngr.getInstance().updateTestWidget(ctx, views);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
resultValue.putExtra(stringID, mAppWidgetId);
}
setResult(RESULT_OK, resultValue);
finish();
}
}
}
我的经理代码
public void updateTestWidget(Context context, RemoteViews remoteViews)
{
String key = context.getString(R.string.TestWidget_string) + "_"
+ AppWidgetManager.EXTRA_APPWIDGET_ID;
String s = getString(context, key, "");
if (s != null && s.equals("") == false)
{
int resID = Integer.valueOf(s);
remoteViews.setImageViewResource(R.id.TestWidgetImage, resID);
}
}
public void pushUpdate(RemoteViews remoteView, Context ctx, Class<?> cls)
{
ComponentName myWidget = new ComponentName(ctx, cls);
AppWidgetManager manager = AppWidgetManager.getInstance(ctx);
manager.updateAppWidget(myWidget, remoteView);
}
推荐答案
问题不是你的configActivity,问题是你的widgetProvider.我遇到了和你一样的问题,但使用 链接解决了 你指定的.您需要在 buildRemoteView 中为您的 configIntent 设置被黑"意图.
The problem isn't your configActivity, the problem is in your widgetProvider. I had the same problem as you did but solved using the link you specified. You need to set the "hacked" intent on your configIntent in buildRemoteView.
这篇关于多个 Android Widget 实例仅更新最后一个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多个 Android Widget 实例仅更新最后一个小部件
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01