gpt4 book ai didi

android - 带有警报管理器的调度程序...重启后不起作用

转载 作者:太空狗 更新时间:2023-10-29 15:54:23 25 4
gpt4 key购买 nike

这是我关于 SO 的第一个问题。我正在制作一个 android 应用程序,它会自动计算通过给出生日来唤醒闹钟的天数。每个预定日期我都有一个通用闹钟时间。

    c8.add(Calendar.MONTH, 18);
sdf = new SimpleDateFormat("yyyy-dd-MM");
//String output = sdf.format(c.getTime());
Date eighteendtmonth= new Date(c8.getTimeInMillis());
dteighteenmonth = sdf.format(eighteendtmonth);
System.out.println("Eighteen Month date is--->"+dteighteenmonth);

c8.set(Calendar.HOUR_OF_DAY, bhour);
c8.set(Calendar.MINUTE, bminute);

try {
Date date1 = df.parse(dteighteenmonth); //birthdate
Date date2 = df.parse(currentdate); //
if(date1.equals(date2) || date1.after(date2))
{
setAlarm(c8, eighteenmonth, dteighteenmonth, alarmtime, name );
}

} catch (ParseException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

private void setAlarm(Calendar targetCal, String detail, String vdate, String vtime, String childname){

alarmid =(int) System.currentTimeMillis();

System.out.println("vdate is--->"+vdate);
System.out.println("alarm time is--->"+vtime);
System.out.println("Alarm is set----->");
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.setAction("android.intent.action.MAIN");
intent.putExtra("detail", detail);
intent.putExtra("childname", name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), alarmid, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);



}

广播接收器...

       public void onReceive(Context context, Intent intent) {

Intent pushIntent = new Intent(context, Childlist.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(pushIntent);

Toast.makeText(上下文, 细节, Toast.LENGTH_LONG).show();

AndroidManifest.xml

 <uses-permission android:name='android.permission.WAKE_LOCK'/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:label="AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" android:process=":remote" />

所以我的问题是我的调度程序运行良好。但在重启设备后,没有一个警报正常工作。帮助我:))

最佳答案

为此,我们需要一个广播接收器来接收“BOOT_COMPLETED”广播。我们必须知道,当设备完成启动时,Android 系统会发送“BOOT_COMPLTED”广播。

在android list 文件中注册BootReciever

               <receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

并且不要忘记在 list 中添加以下权限。

           <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

需要此权限才能收听/想象 BOOT_COMPLETED 操作

启动接收器.java

   public class BootReceiver extends BroadcastReceiver
{

public void onReceive(Context context, Intent intent)
{

// Your code to execute when Boot Completd
**Schedule your Alarm Here**
Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();

}

}

BootReceiver的onRecieve()方法会在开机完成时执行,所以需要把代码写在onreceive()方法里面

关于android - 带有警报管理器的调度程序...重启后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16245412/

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