Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

android - Fire notification at every 24 hours and at exact time of 8 AM

I am using AlarmManager() to fire notification and repeat it at every 24 hours.

My code is on onCreate() in Splash Activity which fires first when anyone opens App. User can install App at anytime. So I want that when User installs App, It checks for the timing and then fires Notification at 8 AM and repeat it daily. I don't want notification when anyone opens App.

My code is as below :

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();
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do as chintan suggested. To get a clear picture, the exact solution might look something similar to the below:

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...