兰州大学本科生毕业论文 基于android平台“每日一句”应用的设计与实现
}
// 更新 widget
appWidgetManager.updateAppWidget(appID, remoteView);
}
@SuppressWarnings(\
private PendingIntent getPendingIntent(Context context, int buttonId) { Intent intent = new Intent();
intent.setClass(context, ExampleAppWidgetProvider.class); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); intent.setData(Uri.parse(\
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0 ); return pi; }
// 调试用:遍历set private void prtSet() {
if (DEBUG) {
int index = 0; int size = idsSet.size();
Iterator
Log.d(TAG, index + \
}
} }
package com.note;
import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder;
40
兰州大学本科生毕业论文 基于android平台“每日一句”应用的设计与实现
import android.util.Log; /*
* @author : skywang
public class ExampleAppWidgetService extends Service {
private static final String TAG=\ // 更新 widget 的广播对应的action private
final
String
ACTION_UPDATE_ALL
\
// 周期性更新 widget 的周期
private static final int UPDATE_TIME = 5000; // 周期性更新 widget 的线程 private UpdateThread mUpdateThread; private Context mContext; // 更新周期的计数 private int count=0;
@Override
public void onCreate() {
// 创建并开启线程UpdateThread mUpdateThread = new UpdateThread(); mUpdateThread.start();
mContext = this.getApplicationContext(); super.onCreate();
}
41
=
兰州大学本科生毕业论文 基于android平台“每日一句”应用的设计与实现
@Override
public void onDestroy(){
// 中断线程,即结束线程。
if (mUpdateThread != null) { mUpdateThread.interrupt(); }
} } /*
* 服务开始时,即调用startService()时,onStartCommand()被执行。 * onStartCommand() 这里的主要作用:
* (01) 将 appWidgetIds 添加到队列sAppWidgetIds中 * (02) 启动线程 */ @Override
public int onStartCommand(Intent intent, int flags, int startId) { }
Log.d(TAG, \
super.onStartCommand(intent, flags, startId); @Override return null;
public IBinder onBind(Intent intent) {
super.onDestroy();
return START_STICKY;
private class UpdateThread extends Thread { @Override public void run() { super.run(); try {
count = 0; while (true) {
Log.d(TAG, \ count++;
42
兰州大学本科生毕业论文 基于android平台“每日一句”应用的设计与实现
Intent updateIntent=new Intent(ACTION_UPDATE_ALL); mContext.sendBroadcast(updateIntent);
Thread.sleep(UPDATE_TIME); }
} catch (InterruptedException e) {
// 将 InterruptedException 定义在while循环之外,意味着抛出 InterruptedException 异常时,终止线程。
e.printStackTrace(); } } } }
43