android 桌面时钟widget 带秒针
本帖最后由 zxb6749 于 2012-11-29 14:29:31 编辑 我做了一个、但是不知道为什么、跑了一会儿后、秒针就不动了、、我看了下、我暂且估计是线程的原因、但是实在没有好的办法、希望高手多多请教、谢谢阿、在线等、
我现在的代码:
package com.viroyal.appwidget.analogClock;
import java.util.Date;
import com.viroyal.appwidget.R;
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.widget.RemoteViews;
public class AnalogClockWidget extends AppWidgetProvider {
private boolean run = true;
private Context mContext;
private int[] Draw = { R.drawable.clock1_second_hand_0,
R.drawable.clock1_second_hand_1, R.drawable.clock1_second_hand_2,
R.drawable.clock1_second_hand_3, R.drawable.clock1_second_hand_4,
R.drawable.clock1_second_hand_5, R.drawable.clock1_second_hand_6,
R.drawable.clock1_second_hand_7, R.drawable.clock1_second_hand_8,
R.drawable.clock1_second_hand_9, R.drawable.clock1_second_hand_10,
R.drawable.clock1_second_hand_11, R.drawable.clock1_second_hand_12,
R.drawable.clock1_second_hand_13, R.drawable.clock1_second_hand_14,
R.drawable.clock1_second_hand_15, R.drawable.clock1_second_hand_16,
R.drawable.clock1_second_hand_17, R.drawable.clock1_second_hand_18,
R.drawable.clock1_second_hand_19, R.drawable.clock1_second_hand_20,
R.drawable.clock1_second_hand_21, R.drawable.clock1_second_hand_22,
R.drawable.clock1_second_hand_23, R.drawable.clock1_second_hand_24,
R.drawable.clock1_second_hand_25, R.drawable.clock1_second_hand_26,
R.drawable.clock1_second_hand_27, R.drawable.clock1_second_hand_28,
R.drawable.clock1_second_hand_29, R.drawable.clock1_second_hand_30,
R.drawable.clock1_second_hand_31, R.drawable.clock1_second_hand_32,
R.drawable.clock1_second_hand_33, R.drawable.clock1_second_hand_34,
R.drawable.clock1_second_hand_35, R.drawable.clock1_second_hand_36,
R.drawable.clock1_second_hand_37, R.drawable.clock1_second_hand_38,
R.drawable.clock1_second_hand_39, R.drawable.clock1_second_hand_40,
R.drawable.clock1_second_hand_41, R.drawable.clock1_second_hand_42,
R.drawable.clock1_second_hand_43, R.drawable.clock1_second_hand_44,
R.drawable.clock1_second_hand_45, R.drawable.clock1_second_hand_46,
R.drawable.clock1_second_hand_47, R.drawable.clock1_second_hand_48,
R.drawable.clock1_second_hand_49, R.drawable.clock1_second_hand_50,
R.drawable.clock1_second_hand_51, R.drawable.clock1_second_hand_52,
R.drawable.clock1_second_hand_53, R.drawable.clock1_second_hand_54,
R.drawable.clock1_second_hand_55, R.drawable.clock1_second_hand_56,
R.drawable.clock1_second_hand_57, R.drawable.clock1_second_hand_58,
R.drawable.clock1_second_hand_59, };
Thread myThread = new Thread() {
public void run() {
while (run) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
showTime(mContext);
}
}
};
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
mContext = context;
showTime(context) ;
myThread.start();
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
run = true;
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
run = false;
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
run = false;
}
private void showTime(Context context) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.clock_widget);
Date dates = new Date();
int sec = dates.getSeconds();
updateViews.setImageViewResource(R.id.clock_second, Draw[sec]);
ComponentName thisWidget = new ComponentName(context,
AnalogClockWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,
updateViews);
}
}
[解决办法]
你启动一个service来更新Appwidget吧,不要用线程
service里
public class UpdateWidgetService extends Service {
AlarmManager alarm;
PendingIntent pintent;
public IBinder onBind(Intent intent) {
return null;
}
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
RemoteViews views = WeatherWidget.getRemoteViews(this);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int[] appids = appWidgetManager.getAppWidgetIds(new ComponentName(this,
WeatherWidget.class));
WeatherWidget.updateAppWidget(views, this, appWidgetManager);
appWidgetManager.updateAppWidget(appids, views);
Date date = new Date();
long now = date.getTime();
//间隔
long unit = 60000;
int s = date.getSeconds();
unit = 60000 - s * 1000;
pintent = PendingIntent.getService(this, 0, intent, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, now + unit, pintent);
}
public void onDestroy() {
if (alarm != null) {
alarm.cancel(pintent);
}
super.onDestroy();
}
}这是我的更新代码,你自己改改
在widget里
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
getRemoteViews(context);
context.startService(new Intent(context,UpdateWidgetService.class));
}
public void onDisabled(Context context) {
super.onDisabled(context);
context.stopService(new Intent(context,UpdateWidgetService.class));
}
