皮卡丘qinqin
public class TimerService extends Service {
private Timer timer;
private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
updateWidget();
}
}, 0, 1000);
}
protected void updateWidget() {
String time=format.format(new Date());
RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.showTime, time);
AppWidgetManager manager=AppWidgetManager.getInstance(getApplicationContext());
ComponentName cn=new ComponentName(getApplicationContext(), WidgetProvider.class);
manager.updateAppWidget(cn, remoteViews);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer.cancel();
}
}