21

我习惯于AlarmManager()每 24 小时触发一次通知并重复一次。

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

我的代码如下:

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

4 回答 4

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);
}
于 2013-06-01T09:32:29.523 回答
10

这是伪代码:

你必须在你的Splash.java

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

第 2 步: false [第 3 步]

Step 2 : true [step 8] (无需设置)

第三步: Get_Time()【用户当前手机时间】

第 4 步: Find_Time_Difference() [此功能将找出用户的移动时间和您的固定时间 (8AM) 之间的差异。]

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

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

第 7 步:它会根据您的固定时间上午 8 点发出警报。

第 8 步:切换您的活动。

于 2013-06-01T08:58:51.417 回答
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);
于 2014-11-23T16:15:33.697 回答
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);
于 2016-01-28T05:51:05.327 回答