本文介绍了在每24小时并在上午8点确切时间火灾通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 AlarmManager()火的通知,并在每24小时重复一次。
I am using AlarmManager() to fire notification and repeat it at every 24 hours.
我的code是在在开机活动的onCreate()这触发第一人时,打开应用程序。用户可以随时安装应用程序。所以,我想,当用户安装应用程序,它会检查的时间,然后在上午8点触发通知,并每天重复它。我不想通知,当有人打开应用程序。
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.
我的code是如下:
public class Splash extends Activity { final String WebsiteURL = "www.mytestbuddy"; 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(); }
}
推荐答案不要为chintan建议。为了获得清晰的画面,精确解可能类似于如下内容:
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); }更多推荐
在每24小时并在上午8点确切时间火灾通知
发布评论