Android的Widget的定时刷新
2016-04-06 22:08
1071 查看
Android的桌面小图标Widget有时候需要定时的刷新,而且updatePeriodMillis属性设置的时间是建议是1个小时。这个时间对有些功能就太长时间了。在开发者文档上面有建议用
AlarmManager下面就是我写的一个小的Demo,让Widget上的ProgressBar以一秒钟progress增加一的速度更新。在onEnable中开一个定时器每秒唤醒WidgetUpdateReceiver一次做一次页面刷新。并且在onDisabled中将它取消。
@Overridepublic void onEnabled(Context context) {// Intent service = new Intent(context,WidgetUpdateService.class);// context.startService(service);long firstTime = SystemClock.elapsedRealtime();firstTime += 1000;Intent receiver = new Intent();receiver.putExtra("FirstTime",firstTime);receiver.setAction(WidgetUpdateReceiver.ACTION);PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 2006, receiver, 0);AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 1000, pendingIntent);Log.i(TAG, "onEnabled: ");super.onEnabled(context);}
@Overridepublic void onDisabled(Context context) {// Intent service = new Intent(context,WidgetUpdateService.class);// context.stopService(service);Intent intent = new Intent();intent.setAction(WidgetUpdateReceiver.ACTION);PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),2006,intent,0);AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);am.cancel(pendingIntent);Log.i(TAG, "onDisabled: ");super.onDisabled(context);}
public class WidgetUpdateReceiver extends BroadcastReceiver {public static String TAG = "WidgetUpdateReceiver";public static final String ACTION = "android.appwidget.action.PROGRESS_UPDATE";@Overridepublic void onReceive(Context context, Intent intent) {Log.i(TAG, "onReceive: ");// receiver.putExtra("FirstTime",firstTime);long currentTime = SystemClock.elapsedRealtime();long firstTime = intent.getLongExtra("FirstTime", SystemClock.elapsedRealtime());int progress = (int)(currentTime-firstTime)/1000;if (progress > 100){Toast.makeText(context,"progress到100了",Toast.LENGTH_SHORT).show();progress = 100;}AppWidgetManager am = AppWidgetManager.getInstance(context);//Widget是在其他应用上面显示的所以要用RemoteViews来管理RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.my_widget);Intent i = new Intent(context,MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(context,2016,i,0);//设置点击图片的点击事件,打开一个ActivityremoteViews.setOnClickPendingIntent(R.id.iv, pendingIntent);remoteViews.setProgressBar(R.id.progressBar,100,progress,false);ComponentName name = new ComponentName(context,MyWidget.class);am.updateAppWidget(name,remoteViews);}}在这个Receiver中就可以做一些刷新Widget的操作。
相关文章推荐
- Android Studio很卡,操作很不顺的点进来!
- Android开发之发送邮件功能的实现(源代码分享)
- android之LayoutInflater
- MSB3073 命令“platforms\android\cordova\clean.bat”已退出,代码为 2。
- Qt for Android开发中消除NDK的黑屏启动页面
- android大文件上传
- On Memory Leaks in Java and in Android.
- Android 引导使用说明、标签功能的封装库
- Android 操作数据库的框架——greenDAO的学习
- Android学习之CardView文档翻译
- Android的桌面小图标Widget的学习
- 开源图片加载库universal-image-loader使用
- Android开发之非Activity类型的Context启动一个目标Activity
- Android--绑定服务调用服务的方法
- Android基础学习第一篇—Project目录结构
- 图片处理问题总结
- startActivityForResult方法的使用
- Android中EditText输入字数统计和限制
- Android绘图基础
- Android Studio SVN使用和VisualSVN-Server配置(图解)