【问题标题】:Android check for system date to change then run codeAndroid 检查系统日期以更改然后运行代码
【发布时间】:2015-02-27 05:19:08
【问题描述】:

在系统日期更改时立即运行代码的最佳方法是什么?

目前我正在尝试根据日期在设备上设置壁纸。如果日期更改,我会将壁纸设置为新图像。我获取和设置墙纸的代码都可以。我正在尝试设置一个警报管理器,但我无法让它工作。如何确保我的闹钟设置为在每次日期更改时立即设置壁纸。

public void onReceive(Context context, Intent intent)
        {
            // TODO Auto-generated method stub
            if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

                alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
                Intent intent1 = new Intent(context, AlarmReciever.class);
                alarmIntent = PendingIntent.getBroadcast(context, 0, intent1, 0);

                alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY,
                        AlarmManager.INTERVAL_DAY, alarmIntent);

                // Execute DownloadImage AsyncTask
                new DownloadImage().execute(URL);

                try {
                    WallpaperManager.getInstance(context).setBitmap(bm);
                }
                catch (  IOException e) {

                }

            }


         }

【问题讨论】:

    标签: android alarmmanager


    【解决方案1】:

    使用此操作在清单文件中注册您的广播接收器:

    android:action="ACTION_DATE_CHANGED"
    

    阅读文档here 了解更多信息。

    【讨论】:

    • 谢谢。我今晚试试这个。
    • 好的,我现在可以检测系统日期更改,但尝试运行我的代码时出错
    • 有没有办法通过 OnReceive() 在设备上设置壁纸?我的异步任务在我的活动中工作,但我想在设备注意到操作更改而不打开应用程序时设置壁纸。
    • 在BroadcastRecceiver的onReceive()中启动一个服务。服务在后台运行。在这里阅读更多:stackoverflow.com/questions/9732812/…
    • 谢谢。非常有用的建议。我会检查一下,所以我将使用警报管理器,然后在接收时运行我的服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多