gpt4 book ai didi

android - 每 24 小时和早上 8 点的确切时间发出火灾通知

转载 作者:IT老高 更新时间:2023-10-28 23:03:39 25 4
gpt4 key购买 nike

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

我的代码在 Splash Activity 中的 onCreate() 上,当任何人打开 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();
}

}

最佳答案

按照 chintan 的建议进行操作。为了获得清晰的图像,确切的解决方案可能类似于以下内容:

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

关于android - 每 24 小时和早上 8 点的确切时间发出火灾通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16870781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com