如何使用服务更新小部件

时间:2018-08-31 11:28:01

标签: java android service widget broadcastreceiver

需要每秒或每分钟更改一次更新时钟小部件的TextView。

我正在从我的AppWidgetProvider的onReceive调用服务:

 private String action = "clock.beautiful.best.com.mmclock.TheService";


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


 //UseThis


    Log.e("h","R");


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

    // Create a fresh intent
    Intent serviceIntent = new Intent(context, TheService.class);

   serviceIntent.setAction(action);

   context.startService(serviceIntent);


    ComponentName componentName = new ComponentName(context, TheService.class);
    AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);




}

我应该怎么做才能及时检查更新,如果有则更新“时间” TextView。

服务:

public class TheService extends Service {

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

@Override
public void onCreate() {

    Log.e("Service","created");



}


@Override
public void onDestroy() {

    Log.e("Service","Destroy");

}

public void changeYexy (){

    RemoteViews remoteViews = new RemoteViews(TheService.this.getPackageName(), R.layout.main_widget);

    remoteViews.setTextViewText(R.id.dateTextView,"T");



}


@Override
public void onStart(Intent intent, int startid) {
    Log.e("Service","start");


    }
}

我不希望用户一次又一次地打开活动以进行更新,我有什么办法可以通过服务或小部件检查和更新时间。.

任何一种都真的很感激

1 个答案:

答案 0 :(得分:0)

您可以使用AlarmManager而不是使用服务来定期更新小部件

public class Alarm
{
    public static void setAlarm(Context context, int interval)
    {
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, yourWidget.class);
        intent.setAction("WIDGET_UPDATE");
        int[] ids = AppWidgetManager.getInstance(context)
                .getAppWidgetIds(new ComponentName(context, yourWidget.class));
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);

        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

        am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval * 1000, pi);
    }

    public void cancelAlarm(Context context)
    {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}

通过这种方式,alram在间隔秒后将“ CIDGET_UPDATE”作为操作将BroadCast发送到小部件。

在小部件的onEnable方法中启用警报:

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

    Alarm.setAlarm(context, 1);

}

在yourWidget的onReceive方法中,更新yourWidget并将警报设置为下一次

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

    super.onReceive(context, intent);

    if ("WIDGET_UPDATE".equals(intent.getAction())) {

        int[] appWidgetIds = AppWidgetManager.getInstance(context)
                    .getAppWidgetIds(new ComponentName(context, yourWidget.class));

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        //set alarm for the next time
        Alarm.setAlarm(context, 1);

        //update your widget here
        onUpdate(context, appWidgetManager, appWidgetIds);      

    }
}

注意:尽管可以使用setRepeating代替setExact,但是对我来说它不能正常工作