我正在研究android appwidget,有定期更新appwidget的问题。将更新周期更改为超过30分钟。仍未更新小部件
public class MyWidgetProvider extends AppWidgetProvider {
final static String ITEM_CLICK_ACTION = "ItemClick";
public static final String TOAST_ACTION = "TOAST_ACTION";
BroadcastReceiver receiver;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Log.e("called", "omupdate");
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_lay);
views.setTextViewText(R.id.textView1,
"Floating Text will be displayed here .. Daily Thoughts");
Intent intent = new Intent(context, RemoteServiceEmin.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.gridview, intent);
// inflating list remote view
Intent intentlist = new Intent(context, ListInflater.class);
intentlist.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
// intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.listview, intentlist);
// setting onclick on gridview
Intent itemClickIntent = new Intent(context, MyWidgetProvider.class);
itemClickIntent.setAction(ITEM_CLICK_ACTION);
itemClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i]);
PendingIntent itemClickPendingIntent = PendingIntent.getBroadcast(
context, 0, itemClickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.gridview,
itemClickPendingIntent);
Intent toastIntent = new Intent(context, MyWidgetProvider.class);
toastIntent.setAction(MyWidgetProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(
context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.ll, toastPendingIntent);
// appWidgetManager.updateAppWidget(appWidgetIds[i], v);
// appWidgetManager.updateAppWidget(appWidgetIds[i], views);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(ITEM_CLICK_ACTION)) {
Bundle extras = new Bundle();
extras.putString("text", intent.getExtras().getString("text"));
Intent actIntent = new Intent();
actIntent.setClass(context, ConfigurationActivity.class);
actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
actIntent.putExtras(extras);
context.startActivity(actIntent);
} else if (intent.getAction().equals(TOAST_ACTION)) {
Toast.makeText(context, "dfjdsfl", Toast.LENGTH_SHORT).show();
} else {
super.onReceive(context, intent);
}
}
}
这是我的appwidget类,我无法跟踪为什么它不会更新
android:updatePeriodMillis="50000"
提前感谢..
答案 0 :(得分:0)
小部件更新的最短实际时间为30分钟,以避免编写较差的程序小部件来干电池。对于小部件的频繁更新,使用AlarmManager来克服这个问题。
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds){
long interval = 50000;
Intent in = new Intent(context, Widgetclass.class);
in.setAction("Widgetupdate");
PendingIntent pi = PendingIntent.getService(context, 0, in, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pi);
//add remaining code here
}
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("widgetupdate")) {
watchWidget = new ComponentName(context,
Widgetclass.class);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_lay);
(AppWidgetManager.getInstance(context)).updateAppWidget(
watchWidget, views);
}
declare this receiver action in menifest file