AndroidWidget实践 --- EverydayTips开发(4)

news/2024/7/18 22:08:11

接下来就是刷新了.刷新操作的话目测有几种方式(目测 --!)

1.在widget创建线程刷新

2.使用timer刷新(其实也是线程吧?)

3.widget连接Service 在Service创建线程刷新

4.widget连接Service 在Service中使用AlarmManager刷新


Thread比较简单,修改widget代码如下

package com.su.tipeveryday; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.provider.ContactsContract.CommonDataKinds.Note; import android.util.Log; import android.widget.RemoteViews; import android.widget.TextView; import android.widget.Toast; public class TipEveryDayWidget extends AppWidgetProvider { private Context mContext; @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {// widget更新执行的方法 // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); // final int N = appWidgetIds.length;//实现单个widget更新的方法,暂时不用 // Log.d("app", "onUpdate--->Ids===" + String.valueOf(N)); // for (int i = 0; i < N; i++) {// 如果有很多同类widget是需要遍历的(他们的id是不同的) // int appWidgetId = appWidgetIds[i]; // updateAppWidget(context, appWidgetManager, appWidgetId);// // 更新widget的方法 // } mContext = context; updateAppWidget(context, appWidgetManager, appWidgetIds);// 更新widget的方法 Thread myThread = new Thread() {//开启线程 public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } mContext.sendBroadcast(new Intent("com.su.ontiprefresh"));// 通知刷新,其实相当于中午说的那个click,同样要在manifest注册 Log.i("SSSSSSS", "Time"); } }; }; myThread.start(); } private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // 1.1,增加跳转用activity相关 intent RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); String tip = Tips.getTips();// 从Tips.java 获取警句 remoteViews.setTextViewText(R.id.textViewWidget, tip);// 设置 Intent intent = new Intent(context, TipEveryDayActivity.class);// 一下三句就可以启动Activity了 intent.putExtra("TIP", tip); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); /** * 传值说明 * ①若该Intent带有数据,则需要将最后一个参数的值设为:FLAG_CANCEL_CURRENT eg: * PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, * fullIntent,PendingIntent.FLAG_CANCEL_CURRENT); * ②若该Intent不带数据,则最后一个参数设为0 eg: PendingIntent * Pfullintent=PendingIntent.getActivity(this, 0, fullIntent, 0); */ remoteViews.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent); Intent intent2 = new Intent("com.su.ontipclick");// 这三句就可以注册按钮事件了,确实比较麻烦 PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, 0); remoteViews.setOnClickPendingIntent(R.id.buttonChange, pendingIntent2); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);// 把widget的内容更新 } public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("com.su.ontipclick")) { Toast.makeText(context, "通过点击刷新", Toast.LENGTH_LONG).show();// 点击按钮会触发的事件 refreshWidget(context); } if (intent.getAction().equals("com.su.ontiprefresh")) { refreshWidget(context); } super.onReceive(context, intent); } private void refreshWidget(Context context) {//抽取了刷新widget的方法 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);// 只能通过远程对象来设置appwidget中的控件状态 String tip = Tips.getTips();// 从Tips.java 获取警句 remoteViews.setTextViewText(R.id.textViewWidget, tip); Intent intent = new Intent(context, TipEveryDayActivity.class);// 重新注册一边开启Activity的事件,因为要获取刷新后的警句 intent.putExtra("TIP", tip); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); remoteViews.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent); AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(context);// 获得appwidget管理实例,用于管理appwidget以便进行更新操作 ComponentName componentName = new ComponentName(context, TipEveryDayWidget.class);// 相当于获得所有本程序创建的appwidget appWidgetManager.updateAppWidget(componentName, remoteViews); } }

不要忘了注册

<receiver android:name=".TipEveryDayWidget" android:label="AAAAAAAATip"> <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="com.su.ontipclick"></action><!-- 单击事件 --> <action android:name="com.su.ontiprefresh"></action><!-- 刷新事件 --> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config" /><!-- 这个是widget的配置文件,出来这个之外其他的不用管直接copy --> </receiver>

这样就可以在桌面每一秒刷换一次警句了


然后我们看看第四种方法(第三种和第一种差不多,就不赘述)


我们需要Service

package com.su.tipeveryday; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.RemoteViews; public class TipService extends Service { private static final String TAG = "TipService"; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onRebind(Intent intent) { super.onRebind(intent); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.i(TAG, "start service Time update"); TipEveryDayWidget.refreshWidget(this); // 设置下次执行时间,每秒刷新一次 long now = System.currentTimeMillis(); long updateMilis = 2000; PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, now + updateMilis, pendingIntent);//now + updateMilis其实是一个死循环 stopSelf(); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } }
然后我们把refreshWidget做了修改 改成public static了,这是不是很喜欢的 感觉有点耦合..同时这里我也罢以前的烂代码精简了一下XD


package com.su.tipeveryday; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.provider.ContactsContract.CommonDataKinds.Note; import android.util.Log; import android.widget.RemoteViews; import android.widget.TextView; import android.widget.Toast; public class TipEveryDayWidget extends AppWidgetProvider { @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {// widget更新执行的方法 // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); refreshWidget(context);// 更新widget的方法 context.startService(new Intent(context, TipService.class)); } public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("com.su.ontipclick")) { Toast.makeText(context, "通过点击刷新", Toast.LENGTH_LONG).show();// 点击按钮会触发的事件 refreshWidget(context); } if (intent.getAction().equals("com.su.ontiprefresh")) { refreshWidget(context); } super.onReceive(context, intent); } public static void refreshWidget(Context context) {// 抽取了刷新widget的方法 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);// 只能通过远程对象来设置appwidget中的控件状态 String tip = Tips.getTips();// 从Tips.java 获取警句 remoteViews.setTextViewText(R.id.textViewWidget, tip); Intent intent = new Intent(context, TipEveryDayActivity.class);// 重新注册一边开启Activity的事件,因为要获取刷新后的警句 intent.putExtra("TIP", tip); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); remoteViews.setOnClickPendingIntent(R.id.textViewWidget, pendingIntent); /** * 传值说明 ①若该Intent带有数据,则需要将最后一个参数的值设为:FLAG_CANCEL_CURRENT eg: * PendingIntent Pfullintent=PendingIntent.getActivity(this, 0, * fullIntent,PendingIntent.FLAG_CANCEL_CURRENT); * ②若该Intent不带数据,则最后一个参数设为0 eg: PendingIntent * Pfullintent=PendingIntent.getActivity(this, 0, fullIntent, 0); */ Intent intent2 = new Intent("com.su.ontipclick");// 这三句就可以注册按钮事件了,确实比较麻烦 PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, 0); remoteViews.setOnClickPendingIntent(R.id.buttonChange, pendingIntent2); AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(context);// 获得appwidget管理实例,用于管理appwidget以便进行更新操作 ComponentName componentName = new ComponentName(context, TipEveryDayWidget.class);// 相当于获得所有本程序创建的appwidget appWidgetManager.updateAppWidget(componentName, remoteViews); } }
效果和以前一样只不过是2s刷新










转载于:https://www.cnblogs.com/sfshine/archive/2012/05/25/2524462.html


http://www.niftyadmin.cn/n/673088.html

相关文章

广电信息:超跌低价本地股 业绩增长190%

http://www.jrj.com  2007年09月10日 17:10 金融界网站 【字体&#xff1a;大 中 小】 【页面调色版 】 周一大盘的表现正如我们上周末所谈到的一样&#xff0c;不排除周一大盘以阳包阴的态势展开。从周一股指止跌后展开的稳步盘升的走势来看&#xff0c;表明当前行情仍处…

Google和百度、雅虎的站内搜索代码

对于一个网站来说&#xff0c;使用搜索引擎来进行站内搜索往往比自己编写的站内搜索更高效&#xff0c;并且不占用网站服务器的资源&#xff0c;下面是我搜集到的几个主要搜索引擎&#xff08;Google和百度、雅虎&#xff09;的站内搜索代码&#xff0c;使用时只需要将代码里的…

Delphi应用程序的调试(五)其他调试工具

Delphi7中提供了一些附加调试工具来帮助用户检查程序错误。从性能上讲&#xff0c;其中一些工具属于高级调试工具。尽管高级调试工具不像其他工具那样常用&#xff0c;但对于经验丰富的编程人员来说&#xff0c;它们是非常有用的。 Evaluate/Modify对话框&#xff08;The Evalu…

TDD从何开始

万事开头难。在TDD中&#xff0c;人们纠结最多的可能是这样一个问题&#xff1a;如何写第一个测试呢&#xff1f;实际上要视不同的问题而定。如果问题本身是一个算法求解&#xff0c;或者是一个大系统中的小单元&#xff0c;那么可以从最简单、最直观的情况出发&#xff0c;这样…

触发器不能读它的问题

http://space.itpub.net/7728585/viewspace-718992 报错如下&#xff1a; SQL> update GPPAYMENTFUND set attribute51 where fundapplyno 20120314500102010001; update GPPAYMENTFUND set attribute51 where fundapplyno 20120314500102010001 ORA-04091: 表 ACDEP.GPPAYM…

北京银行(601169))今日申购全攻略

2007-9-11 8:28:00 代码:601169 作者:来源: 出处: 顶点财经加入收藏复制链接给好友跳到低部北京银行(601169) 新股发行网上申购日:2007-09-11&#xff0c;发行方式:网下向询价对象配售和网上定价发行相结合&#xff0c;发行数量:12亿股&#xff0c;申购上限&#xff1a;8.4亿股…

UVa 409 - Excuses, Excuses!

1A,呵呵。 注意不区分大小写&#xff0c;如果单词重复也要算的&#xff0c;还有单词要合法&#xff0c;就是哲句话“A keyword occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line o…

vim按键功能总览表格

转载于:https://www.cnblogs.com/CodeWorkerLiMing/archive/2012/06/09/2543328.html