我有一个在主 Activity 中声明和编辑的字符串变量。
我希望它显示在小部件中,但不确定如何显示。
在我的 Widget 文件中:
remoteViews.setTextViewText(R.id.widgetAmount, String.valueOf(number))
但是数字变量是在另一个类中声明的。
最佳答案
您应该使用 Intent
发送一个 Broadcast
,其中包含您想要在 App Widget 中显示的值。创建一个 Intent
并添加值:
Intent intent = new Intent(context, YourAppWidgetProvider.class);
intent.putExtra("extra_value", value);
sendBroadcast(intent);
现在在您的 AppWidgetProvider
类的 onReceive 中您可以访问这个值:
String value = intent.getStringExtra("extra_value");
希望这对您有所帮助。
关于java - 在 Android Widget 中显示一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9466148/