我在这里看到了很多答案,但没有一个有效......我只想在我点击旁边的按钮后立即更新textview。我试图打印一个toast消息,它正常显示,但更新视图是问题。
我的代码
public class FollowersWidget extends AppWidgetProvider {
public static String REFRESH_ACTION = "android.appwidget.action.APPWIDGET_UPDATE";
int x = 0;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(REFRESH_ACTION)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
x++;
views.setTextViewText(R.id.textv_count, x + "");
int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, FollowersWidget.class));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
try {
String count = MyConfigureActivity.getNumCount(context, appWidgetId);
views.setTextViewText(R.id.textv_count, count );
views.setOnClickPendingIntent(R.id.refreshButton,getRefreshPendingIntent(context, appWidgetId));
} catch (Exception e) {
e.printStackTrace();
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
public static PendingIntent getRefreshPendingIntent(Context context, int appWidgetId) {
Intent intent = new Intent(REFRESH_ACTION);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
// other widget methods..
..
.
}
我的清单
<receiver android:name=".FollowersWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/followers_info"/>
</receiver>
答案 0 :(得分:0)
请查看以下代码:
public class WidgetExample extends AppWidgetProvider {
public static final String REFRESH_ACTION = "YOUR_REFRESH_ACTION";
public static final String VALUE_KEY = "VALUE_KEY";
public static final String PREFERENCE_FILE_KEY = "PREFERENCE_FILE_KEY";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent != null) {
if (intent.getAction() != null) {
if (intent.getAction().equals(REFRESH_ACTION)) {
int value = readValue(context) + 1;
saveValue(context , value);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetExample.class));
for (int id : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
views.setTextViewText(R.id.textv_count, String.valueOf(value));
views.setOnClickPendingIntent(R.id.button_refresh, getRefreshPendingIntent(context));
appWidgetManager.updateAppWidget(id, views);
}
}
}
}
}
@Override
public void onEnabled(Context context) {
int value = readValue(context);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetExample.class));
for (int id : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
views.setTextViewText(R.id.textv_count, String.valueOf(value));
views.setOnClickPendingIntent(R.id.button_refresh, getRefreshPendingIntent(context));
appWidgetManager.updateAppWidget(id, views);
}
}
private void saveValue(Context context, int value) {
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_FILE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(VALUE_KEY, value);
editor.apply();
}
private int readValue(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_FILE_KEY, Context.MODE_PRIVATE);
return sharedPref.getInt(VALUE_KEY, 0);
}
public static PendingIntent getRefreshPendingIntent(Context context) {
Intent intent = new Intent(REFRESH_ACTION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
答案 1 :(得分:0)
我终于明白了,我已经看到很多方法来处理onClick on小部件,但这种方式对我来说很有效,并且它会在视觉上更新。
<强>代码强>
public class TestWidget extends AppWidgetProvider {
public static String REFRESH_ACTION = "android.appwidget.action.APPWIDGET_UPDATE";
static private RemoteViews views;
private int x = 0;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equalsIgnoreCase(REFRESH_ACTION)) {
views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TestWidget.class));
x++;
views.setTextViewText(R.id.textv_count, x + "");
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
views.setTextViewText(R.id.textv_count, x + "");
views.setOnClickPendingIntent(R.id.button, getPenIntent(context));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
static private PendingIntent getPenIntent(Context context) {
Intent intent = new Intent(context, TestWidget.class);
intent.setAction(REFRESH_ACTION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}