每24小时和上午8点准确的时间点火

时间:2013-06-01 08:34:19

标签: android alarmmanager

我正在使用AlarmManager()来发送通知,并在每24小时重复一次。

我的代码位于Splash Activity中的onCreate(),当有人打开App时,它会先触发。用户可以随时安装App。所以我希望当用户安装应用程序时,它会检查时间,然后在上午8点触发通知并每天重复。当有人打开App时,我不想要通知。

我的代码如下:

public class Splash extends Activity {

final String WebsiteURL = "http://www.mytestbuddy.com";

String cookie;

@SuppressLint("SimpleDateFormat")
@Override
protected void onCreate(Bundle showSplash) {
    // TODO Auto-generated method stub
    super.onCreate(showSplash);
    setContentView(R.layout.splash);

    getWindow().getDecorView().setBackgroundColor(Color.WHITE);

    Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);

    final PendingIntent pendingIntent = PendingIntent.getBroadcast(
            Splash.this, 0, myIntent, 0);

    final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    CookieSyncManager.createInstance(this);
    CookieManager cm = CookieManager.getInstance();
    cm.setAcceptCookie(true);
    CookieSyncManager.getInstance().sync();
    if (cm.getCookie("" + WebsiteURL + "") != null) {
        cookie = cm.getCookie("" + WebsiteURL + "").toString();
    } else {
        cookie = null;
    }

    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (cookie == null) {
                    Intent openStartingPoint = new Intent(
                            "com.MobileWeb.mytestbuddy.Login");
                    startActivity(openStartingPoint);
                } else if (cookie.contains("Premium")) {

                    Calendar firingCal = Calendar.getInstance();
                    Calendar currentCal = Calendar.getInstance();

                    firingCal.set(Calendar.HOUR, 10);
                    firingCal.set(Calendar.MINUTE, 30);
                    firingCal.set(Calendar.SECOND, 0);

                    long intendedTime = firingCal.getTimeInMillis();
                    long currentTime = currentCal.getTimeInMillis();

                    if (intendedTime >= currentTime) {
                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);

                    } else {
                        firingCal.add(Calendar.DAY_OF_MONTH, 1);
                        intendedTime = firingCal.getTimeInMillis();

                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);
                    }

                    Intent openStartingPoint = new Intent(
                            "com.MobileWeb.mytestbuddy.PremiumMain");
                    startActivity(openStartingPoint);
                } else {

                    Calendar firingCal = Calendar.getInstance();
                    Calendar currentCal = Calendar.getInstance();

                    firingCal.set(Calendar.HOUR, 10);
                    firingCal.set(Calendar.MINUTE, 30);
                    firingCal.set(Calendar.SECOND, 0);

                    long intendedTime = firingCal.getTimeInMillis();
                    long currentTime = currentCal.getTimeInMillis();

                    if (intendedTime >= currentTime) {
                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);

                    } else {
                        firingCal.add(Calendar.DAY_OF_MONTH, 1);
                        intendedTime = firingCal.getTimeInMillis();

                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);
                    }

                    Intent openStartingPoint = new Intent(
                            "com.MobileWeb.mytestbuddy.Main");
                    startActivity(openStartingPoint);
                }
            }
        }
    };

    timer.start();
}

}

4 个答案:

答案 0 :(得分:32)

按照chintan的建议。为了获得清晰的图片,exact solution可能与下面的内容类似:

Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second

long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();

if(intendedTime >= currentTime){ 
   // you can add buffer time too here to ignore some small differences in milliseconds
   // set from today
   alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
} else{
   // set from next day
   // you might consider using calendar.add() for adding one day to the current day
   firingCal.add(Calendar.DAY_OF_MONTH, 1);
   intendedTime = firingCal.getTimeInMillis();

   alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}

答案 1 :(得分:10)

这是伪代码:

您必须在Splash.java

中写下此代码

第1步: Is_Alarm_Set(); [布尔]

第2步: false [第3步]

第2步: true [第8步](无需设置)

第3步: Get_Time() [用户当前的移动时间]

第4步: Find_Time_Difference() [此功能可以找到用户移动时间与修复时间(上午8点)之间的差异。]

步骤5:现在根据时差设置闹钟。[即当前时间是晚上7点,日期是1月6日,然后在第二天将闹钟设置为上午8点。]

第6步:设置重复天数setRepeating()

第7步:根据您的修正时间早上8点发出警报。

第8步:切换您的活动。

答案 2 :(得分:2)

Appu的答案给了我很多帮助但是如果你想要一个固定版本的它更具可读性和短缺的话看看:

PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR_OF_DAY, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second

if(firingCal.compareTo(currentCal) < 0) { 
   firingCal.add(Calendar.DAY_OF_MONTH, 1);
} 
Long intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime , AlarmManager.INTERVAL_DAY, pendingIntent);

答案 3 :(得分:2)

// Retrieve a PendingIntent that will perform a broadcast
            Intent alarmIntent = new Intent(HomeContactActivity.this,
                    AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(
                    HomeContactActivity.this, 0, alarmIntent, 0);

            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            // Set the alarm to start at 10:00 AM
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 10);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);




            manager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 86400000, // for repeating
                                                            // in every 24
                                                            // hours
                    pendingIntent);