大家好我在我的应用程序中使用过widget,我想在点击按钮时从活动中更新它,我写了这段代码但是小部件没有更新,请帮助我,谢谢。
AppWidgetProvider中的代码:
@Override
public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
current_routine = prefs.getInt("current_routine", -1);
dabase dabase = new dabase(context);
String name = dabase.get_routine_name(current_routine);
DateFormat df = new SimpleDateFormat("EEE");
day3 = df.format(Calendar.getInstance().getTime());
DateFormat df3 = new SimpleDateFormat("dd/MM/yyyy");
String day2 = df3.format(Calendar.getInstance().getTime());
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
appWidgetId = appWidgetIds[i];
remoteViews.setTextViewText(R.id.widget_routine, name);
remoteViews.setInt(R.id.widget_routine, "setPaintFlags", Paint.UNDERLINE_TEXT_FLAG);
System.out.println(name + "maaaaaaaaaaaanas ");
remoteViews.setTextViewText(R.id.widget_date, day3 + " " + day2);
Intent svcIntent = new Intent(context, WidgetService.class);
//passing app widget id to that RemoteViews Service
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//setting a unique Uri to the intent
//don't know its purpose to me right now
svcIntent.setData(Uri.parse(
svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
//setting adapter to listview of the widget
remoteViews.setRemoteAdapter(R.id.widget_recycle,
svcIntent);
//setting an empty view in case of no data
remoteViews.setEmptyView(R.id.widget_recycle, R.id.empty_view);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
on Recive:
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("RRRERERER1312");
if (ACTION_UPDATE.equals(intent.getAction())) {
Toast.makeText(context, "Hello world from widget", Toast.LENGTH_LONG).show();
System.out.println("RRRERERER");
remoteViews.setTextViewText(R.id.widget_date, "TTTTTTTT");
AppWidgetManager.getInstance(context).updateAppWidget(
new ComponentName(context, Widget.class), remoteViews);
} else {
super.onReceive(context, intent);
}
}
in Manifest:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.UPDATE_MY_WIDGET"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
信息中的:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget"
android:initialLayout="@layout/widget"
android:minWidth="250dp"
android:minHeight="205dp"
android:previewImage="@mipmap/ic_launcher"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="1800000"
android:widgetCategory="keyguard|home_screen"
>
</appwidget-provider>
在活动中:
> Intent intent = new Intent(this, Widget.class);
> intent.setAction(Widget.ACTION_UPDATE);
> int ids[] = AppWidgetManager.getInstance(
> getApplication()).getAppWidgetIds(new ComponentName(getApplication(), Widget.class));
> intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
> sendBroadcast(intent);
答案 0 :(得分:0)
这里的最新答案,但是您无需重写AppWidgetProvider中的onReceive方法即可直接从您的活动中更新appwidget。我这样调用onUpdate方法:
int[] ids = AppWidgetManager.getInstance(getApplication()).
getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
MyWidget myWidget = new MyWidget();
myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);