简体   繁体   English

更改日期时更新小部件

[英]Update widget on day change

I have a widget that displays the current date among other things. 我有一个可显示当前日期的小部件。 However the widget only gets updated when a user performs an action in the main activity. 但是,仅当用户在主要活动中执行操作时,小部件才会更新。

How would I go about forcing the widget to update the next time the phone is turned on after midnight to reflect the new date? 我将如何强迫小部件在午夜后下次打开手机时进行更新以反映新日期?

I understand I may need an AlarmManager but I'm not clear on the parameters. 我知道我可能需要AlarmManager,但是我不清楚参数。 And do I put it in my main activity and have it call the method that calls update on my widget? 我是否将其放在我的主要活动中,并让其调用在小部件上调用update的方法?

in a similar style, I have a service that I want to run at a certain time. 以类似的方式,我有一个要在特定时间运行的服务。 when this service has done it's work it then updates my widget and stops itself (the service). 该服务完成后,它便会更新我的小部件并停止自身(该服务)。 here is a snippet of how I launch my service at a specified time 这是我在指定时间启动服务的代码段

Intent mSrvcIntent = new Intent(packageContext, MyService.class);
PendingIntent mSrvcPendingingIntent = PendingIntent.getService(packageContext,0, mSrvcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mAlarmManager = (AlarmManager)context.getSystemService(android.content.Context.ALARM_SERVICE);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time to start service, mSrvcPendingingIntent);

I hope this is near enough for you to be able to solve your problem 我希望这足够使您能够解决您的问题

ad this to your manifest widget receiver 将其投放到清单小部件接收者

 <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="android.intent.action.DATE_CHANGED" />...

onReceive() will fire onReceive()将触发

This worked for me: 这对我有用:

<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="android.intent.action.DATE_CHANGED"/>
    <action android:name="android.intent.action.TIME_SET"/>
</intent-filter>

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

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