Android Widget更新

时间:2011-02-01 18:09:22

标签: android widget

基于this tutorial我创建了一个应该显示时间的小部件。 java方式有效,但服务方式没有。

HelloWidget.java:

public class HelloWidget extends AppWidgetProvider {

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

        Intent intent = new Intent(context, UpdateService.class);
        context.startService(intent);
    }
}

UpdateService.java:

public final class UpdateService extends Service {
    @Override
    public void onStart(Intent intent, int startId) {
        RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);

        Date date = new Date();
        java.text.DateFormat format = SimpleDateFormat.getTimeInstance(
                SimpleDateFormat.MEDIUM, Locale.getDefault());
                updateViews.setTextViewText(R.id.widget_textview, "Current Time " + format.format(date));

        ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

hello_widget_provider.xml 包含以下行:android:updatePeriodMillis="1000"

问题:

小部件显示当前时间,但它没有更新(java方式)。

我的主要目标是更新小部件,例如每天18:56。 Idk如果这是好方法,但我试图像下面那样修改onUpdate方法,但它有ALARM_SERVICE的问题:ALARM_SERVICE无法解析为变量

public class HelloWidget extends AppWidgetProvider {

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

        AlarmManager alarmManager;
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
          intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 18);
        cal.set(Calendar.MINUTE, 56);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400, pendingIntent);
   }
}

感谢任何帮助。

2 个答案:

答案 0 :(得分:16)

回顾我问这个问题的时间让我意识到我在过去的1。5年里学到了多少。

由于我刚刚在过去两周内使用过小部件,我刚刚发现了我的这个未解决的问题,我决定解决它。我希望它能帮助别人。

AppWidgetProvider应如下所示:

public class MyWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
    private PendingIntent service = null;  

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


           final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

           final Calendar TIME = Calendar.getInstance();
           TIME.set(Calendar.MINUTE, 0);
           TIME.set(Calendar.SECOND, 0);
           TIME.set(Calendar.MILLISECOND, 0);

           final Intent i = new Intent(context, MyService.class);

           if (service == null)
           {
               service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
           }

           m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 1, service);

    }

      @Override
       public void onDeleted(Context context, int[] appWidgetIds) {
               super.onDeleted(context, appWidgetIds);
       }

       @Override
       public void onDisabled(Context context) {
               super.onDisabled(context);

               final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
               if (service != null)
               {
                   m.cancel(service);
               }
       }

       @Override
       public void onEnabled(Context context) {
               super.onEnabled(context);
       }

       @Override
       public void onReceive(Context context, Intent intent) {

         super.onReceive(context, intent);

       }

}

服务:

public class MyService extends Service
{
    @Override
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        buildUpdate();

        return super.onStartCommand(intent, flags, startId);
    }

    private void buildUpdate()
    {
        String lastUpdated = DateFormat.format("hh:mm:ss", new Date()).toString();

        RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_layout);
        view.setTextViewText(R.id.btn_widget, lastUpdated);

        ComponentName thisWidget = new ComponentName(this, MyWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, view);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}

另外,在Manifest中注册提供者和服务:

<receiver android:name="MyWidget" android:label="Tutorial_Widget 1x1">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.something.MyWidget.ACTION_WIDGET_RECEIVER"/>
            </intent-filter>    
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
        </receiver>

         <service android:enabled="true" android:name=".MyService" />

答案 1 :(得分:3)

ALARM_SERVICE是Context类的静态字段。因此,键入Context.ALARM_SERVICE或使用静态导入。

请记住,当您设置updatePeriodMillis属性值时,不能保证您的onUpdate方法完全被调用。