如何自动更新窗口小部件才能自动更新(不是通过用户点击)?
这是我的代码:
public class Widget extends appwidgetprovider {private static final String ACTION_UPDATE = AppWidgetManager.ACTION_APPWidget_UPDATE;@OverrIDepublic voID onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIDs) { Componentname cn = new Componentname(context,Widget.class); appWidgetManager.updateAppWidget(cn,remoteVIEw(context));}@OverrIDepublic voID onReceive(Context context,Intent intent) { super.onReceive(context,intent); if (intent.getAction().equalsIgnoreCase(ACTION_UPDATE)) { Componentname cn = new Componentname(context,Widget.class); AppWidgetManager.getInstance(context).updateAppWidget(cn,remoteVIEw(context)); Intent launch = new Intent(context,Main.class); PendingIntent pi = PendingIntent.getActivity(context,launch,0); try { pi.send(); } catch (CanceledException e) { e.printstacktrace(); } }}private RemoteVIEws remoteVIEw(Context ctx) { ArrayList<Integer> imageID = new ArrayList<Integer>(); for (int i = 1; i <= 30; i++) { int drawableImageID = ctx.getResources().getIDentifIEr("image" + i,"drawable",ctx.getPackagename()); imageID.add(drawableImageID); } RemoteVIEws rv = new RemoteVIEws(ctx.getPackagename(),R.layout.Widget); Calendar todayDate = Calendar.getInstance(); todayDate.setTimeInMillis(System.currentTimeMillis()); Calendar startDate = Calendar.getInstance(); startDate.set(2014,05,15); int dayDiffer = Helper.getDiffernce(todayDate,startDate); dayDiffer += 1; if (dayDiffer >= 1 && dayDiffer <= 30) { rv.setInt(R.ID.img_Widget,"setimageResource",imageID.get(dayDiffer - 1)); T(ctx,"in >> " + dayDiffer); } else if (dayDiffer >= 0) { T(ctx,"before >> " + dayDiffer); } else if (dayDiffer <= 31) { T(ctx,"after >> " + dayDiffer); } Intent update = new Intent(ctx,Widget.class); update.setAction(ACTION_UPDATE); PendingIntent pi_update = PendingIntent.getbroadcast(ctx,update,0); rv.setonClickPendingIntent(R.ID.img_Widget,pi_update); return rv;}public voID T(Context context,String string) { Toast.makeText(context,string,Toast.LENGTH_SHORT).show();}
}
解决方法 更改您的“remoteVIEw()”方法中的PendingIntent:Intent userinput = new Intent(ctx,Widget.class);userinput.setAction("LAUNCH_MAIN_ACTIVITY"); // use another action!PendingIntent pi_userinput = PendingIntent.getbroadcast(ctx,userinput,0);rv.setonClickPendingIntent(R.ID.img_Widget,pi_userinput);
您的新“onReceive()”方法可能如下所示:
@OverrIDepublic voID onReceive(Context context,intent); if (intent.getAction().equalsIgnoreCase(ACTION_UPDATE)) { Componentname cn = new Componentname(context,remoteVIEw(context)); } // the launch will always be executed,// the update only for "ACTION_UPDATE" Intent launch = new Intent(context,Main.class); PendingIntent pi = PendingIntent.getActivity(context,0); try { pi.send(); } catch (CanceledException e) { e.printstacktrace(); }}
希望这可以帮助!
总结以上是内存溢出为你收集整理的android – 在Appwidget中的一个视图中使用两个pendingIntent全部内容,希望文章能够帮你解决android – 在Appwidget中的一个视图中使用两个pendingIntent所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)