简体   繁体   English

无法使用AlarmManager和日历更新Android窗口小部件

[英]Cannot update Android Widget using AlarmManager and Calendar

I want to update my widget but it simply doesn't work! 我想更新我的小部件,但是根本不起作用! I made it so that the update happens at particular time - a few minutes after I run the app, but widget doesn't get updated. 我这样做是为了让更新在特定的时间发生-在我运行该应用后几分钟,但是小部件没有得到更新。 I know this because I am setting the label with the System.currentTimeMillis. 我知道这是因为我正在使用System.currentTimeMillis设置标签。 When app gets updated this label should have a new values. 当应用更新时,此标签应具有新值。 I did everything as the Android AlarmManager document told me. 我做了一切,就像Android AlarmManager文档告诉我的那样。

views.setTextViewText(R.id.todayDate,System.currentTimeMillis()+"");

So here is what I have done, in both onReceive and onUpdate of 所以这是我在onReceive和onUpdate中所做的

public class CalendarWidget extends AppWidgetProvider 

More Code 更多代码

Calendar todayMidnight = Calendar.getInstance();
todayMidnight.setTimeInMillis(System.currentTimeMillis());
todayMidnight.add(Calendar.DATE, 0);
todayMidnight.set(Calendar.HOUR_OF_DAY, 23);
todayMidnight.set(Calendar.MINUTE, 2);
todayMidnight.set(Calendar.SECOND, 0);

AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent (context, CalendarWidget.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

alarm.setRepeating(AlarmManager.RTC, todayMidnight.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

... ...

 views.setTextViewText(R.id.todayDate,System.currentTimeMillis()+"");

In Manifest 在清单中

<receiver
            android:name="com.standardSPenPlannerInhoHwang.CalendarWidget"
            android:theme="@android:style/Theme.Translucent" 
            android:process=":remote"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.intent.action.TIMEZONE_CHANGED" />
                <action android:name="android.intent.action.TIME_SET" />
                <action android:name="android.intent.action.DATE_CHANGED" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@layout/calendar_widget2" />
        </receiver>

But app doesn't get updated at 11:02PM!! 但是应用程序在晚上11:02不会更新! This should work, right? 这应该工作,对不对? (Just note from me updating the widget from my activities work fine. It is just alarm manager that doesn't update the widget!) (请注意,从我的活动中更新窗口小部件可以正常工作。只是警报管理器不会更新窗口小部件!)

is CalendarWidget your receiver class? CalendarWidget是您的接收者类吗?
you need a receiver class 你需要一个接收器类
public class AlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { //Alarm received,update your widget } }

and in your activity: 在您的活动中:
Intent alarmIntent=new Intent(yourActivity.this,AlarmReceiver.class); PendingIntent pi=PendingIntent.getActivity(context,ID,intent,0); AlarmManager manager =(AlarmManager)getSystemService(Context.ALARM_SERVICE); manager.set(AlarmManager.RTC, ... ,pendingIntent);

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

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