Android AppWidgetProvider 定时刷新问题

今天按照相关教材学习AppWidgetProvider,但是不知道为什么程序无法自动刷新,相关程序如下:
appwidgetTest.java

import java.text.SimpleDateFormat;
import java.util.Date;
import Android.app.Service;

 

import Android.appwidget.AppWidgetManager;
import Android.appwidget.AppWidgetProvider;
import Android.content.ComponentName;
import Android.content.Context;
import Android.content.Intent;
import Android.os.IBinder;
import Android.widget.RemoteViews;
public class appwidgetTest extends AppWidgetProvider {
    /** Called when the activity is first created. */
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds){
Intent intent = new Intent(context,UpdateService.class);
context.startService(intent);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public static class UpdateService extends Service{

public void onStart(Intent intent,int startId){
  super.onStart(intent,startId);
  RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.main);
  SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
  updateViews.setTextViewText(R.id.TextView01,""+sdf.format(new Date()));
 
  ComponentName thisWidget = new ComponentName(this,appwidgetTest.class);
  AppWidgetManager manager = AppWidgetManager.getInstance(this);
  manager.updateAppWidget(thisWidget, updateViews);
}
@Override
public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.android.com/apk/res/android"
      package="com.hyz"
      Android:versionCode="1"
      Android:versionName="1.0">
    <application Android:icon="@drawable/icon" android:label="@string/app_name">
            <receiver Android:label="@string/app_name"
                   Android:name=".appwidgetTest">
            <intent-filter>
            <action Android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
            </intent-filter>
            <meta-data Android:name="android.appwidget.provider" android:resource="@xml/my_provider"></meta-data>
            </receiver>
            <service Android:name=".appwidgetTest$UpdateService"/>
    </application>
</manifest>

my_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
  xmlns:Android="http://schemas.android.com/apk/res/android"
  Android:minWidth="72dip"
  Android:minHeight="72dip"
  Android:updatePeriodMillis="1000"
  Android:initialLayout="@layout/main"
  > 
</appwidget-provider>

完全无视我设置的Android:updatePeriodMillis="1000",请教是哪里出错了?完全是按照相关教材来的。

解决方法:

网上找了资料,发现原来1.5以后的版本就不支持刷新了,要自己手写一个timer来刷新程序,具体如下:

appwidgetTest .class

public class appwidgetTest extends AppWidgetProvider {

/** Called when the activity is first created. */

public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds){

Timer timer = new Timer();

timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 1000);

}

}

MyTime.class

public class MyTime extends TimerTask{

 

RemoteViews remoteViews;

AppWidgetManager appWidgetManager;

ComponentName thisWidget;

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

public MyTime(Context context,AppWidgetManager appWidgetManager){

this.appWidgetManager = appWidgetManager;

remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);

thisWidget = new ComponentName(context,appwidgetTest.class);

}

@Override

public void run() {

// TODO Auto-generated method stub

remoteViews.setTextViewText(R.id.TextView01,""+sdf.format(new Date()));

appWidgetManager.updateAppWidget(thisWidget, remoteViews);

}

 

}

其它配置不变


你可能感兴趣的:(android,timer,service,application,Class,encoding)