我的应用小部件有一个问题:它永远不会更新。如果我在调试器上设置断点,我可以看到它有效,但如果用户在其设备上使用它,则刷新不起作用。
从应用活动的代码手动触发刷新工作,并在创建时刷新。
以下是我的小部件的代码:
public class WidgetClass extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
for(int i=0; i<appWidgetIds.length; i++){
int currentWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, WidgetClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
//PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 1000*15;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widgetlayout);
views.setOnClickPendingIntent(R.id.stationlayout, pending);
Log.d("Widget Refresh", " Refresh");
//LISTENER TO OPEN THE APP ON CLICK
Intent clickIntent = new Intent(context, SplashScreen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
views.setOnClickPendingIntent(R.id.widgetstationlayout, pendingIntent);
//this refresh the content of the widget
new AsyncConnectWidget(context,MainActivity.getDB(),MainActivity.sql,null,views,currentWidgetId,appWidgetManager).execute();
}
}
}
我将其设置为每30分钟刷新一次,这是最小间隔:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widgetlayout"
android:minHeight="120dp"
android:minWidth="280dp"
android:updatePeriodMillis="1800000"
android:widgetCategory="home_screen">
</appwidget-provider>
这就是它在清单上的设置方式:
<receiver
android:icon="@drawable/appicon"
android:label=".name_of_my_widget"
android:name=".WidgetClass" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
答案 0 :(得分:0)
我的不好:我没有超越OnUpdate
方法。我不知道为什么它在开始时被成功调用了。