简体   繁体   English

如何使用服务更新小部件

[英]How to update a widget using a service

Need to update TextView of a clock widget every second or every time the minute changes .. 需要每隔一秒或每分钟更改一次时更新时钟小部件的TextView。

I am calling the service from onReceive of my AppWidgetProvider : 我正在从我的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);




}

What should i do to check for update in time and if there is a then update the 'time' TextView.. 我应该怎么做才能及时检查更新,如果有,然后更新“时间” TextView。

Service : 服务内容:

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");


    }
}

I don't want users to open the activity again and again to update , is there any way i can check and update the time from service or widget.. 我不希望用户一次又一次地打开活动以进行更新,有什么办法可以检查和更新服务或窗口小部件的时间。

Any kind of is really really really appreciated 真的很感激任何一种

Instead of Using service you can use AlarmManager to update widget periodically 代替使用服务,您可以使用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);
    }
}

in this way the alram send a broadCast to widget with "WIDGET_UPDATE" as action after interval seconds. 通过这种方式,alram在间隔秒后将“ CIDGET_UPDATE”作为操作发送到广泛的窗口小部件。

Enable the alarm in onEnable method of your widget: 在小部件的onEnable方法中启用警报:

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

    Alarm.setAlarm(context, 1);

}

In the onReceive method of yourWidget update yourWidget and set the alarm for next time 在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);      

    }
}

Note : although it is possile to use setRepeating instead of setExact but for me it does not work properly 注意 :虽然可以使用setRepeating代替setExact,但是对我来说它不能正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM