gpt4 book ai didi

android - 只更新一个 AppWidget 实例

转载 作者:太空狗 更新时间:2023-10-29 15:18:33 31 4
gpt4 key购买 nike

我正在创建带有可点击按钮的 Android 主屏幕小部件。单击按钮后,我将使用来自 SharedPreferences 的数据更新 TextView。我不知道我做错了什么,但是单击按钮后,每个小部件实例都会更新。如何只更新点击的小部件?

测试小部件配置:

public class TestWidgetConfiguration extends Activity {
Button confirm;
EditText edit;
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setResult(RESULT_CANCELED);
setContentView(R.layout.testwidgetconfiguration);

confirm = (Button)findViewById(R.id.button1);
confirm.setOnClickListener(confirmListener);

edit = (EditText)findViewById(R.id.editText1);

Intent intent = getIntent();
Bundle extras = intent.getExtras();

if(extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}

if(mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}

private Button.OnClickListener confirmListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

SharedPreferences prefs = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("id:"+mAppWidgetId, Integer.parseInt(edit.getText().toString()));
editor.commit();

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(TestWidgetConfiguration.this);
TestWidget.updateAppWidget(TestWidgetConfiguration.this, appWidgetManager, mAppWidgetId);

setResult(RESULT_OK, resultValue);
finish();
}
};
}

测试小部件:

public class TestWidget extends AppWidgetProvider {
public static String ACTION = "TestAction";
public static String URI_SCHEME ="ASD";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);

Toast.makeText(context, "onUpdate()", Toast.LENGTH_SHORT).show();
}

public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences("prefs", 0);
int id = prefs.getInt("id:"+appWidgetId, 0);

RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.testwidget);
updateViews.setTextViewText(R.id.textView2, "[" + String.valueOf(appWidgetId) + "]" + id);

Intent intent = new Intent(context, TestWidget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setAction(ACTION);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, 0);
//Uri data = Uri.withAppendedPath(
// Uri.parse(URI_SCHEME + "://widget/id/")
// ,String.valueOf(appWidgetId));
//intent.setData(data);

updateViews.setOnClickPendingIntent(R.id.button1, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);

Toast.makeText(context, "updateAppWidget(): " + String.valueOf(appWidgetId) + "\n", Toast.LENGTH_LONG).show();
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);

Toast.makeText(context, "onReceive()", Toast.LENGTH_SHORT).show();

if (ACTION.equals(intent.getAction())) {
int idd = 0;
Bundle extras = intent.getExtras();

if(extras != null) {
idd = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
}

Toast.makeText(context, "id: " + idd, Toast.LENGTH_SHORT).show();

SharedPreferences prefs = context.getSharedPreferences("prefs", 0);
int id = prefs.getInt("id:"+idd, 0);

RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.testwidget);
rv.setTextViewText(R.id.textView1, "lala" + id);

ComponentName testWidget = new ComponentName(context, TestWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(testWidget, rv);
}
}
}

最佳答案

TestWidget#onReceive 中更改以下行:

manager.updateAppWidget(testWidget, rv);

manager.updateAppWidget(idd, rv);

它是 idd 而不是 Ziem 在评论中写的 id。

关于android - 只更新一个 AppWidget 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10162086/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com