Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduler with Alarm Manager... doesnt work after reboot

This is my first question on SO. I am making an android app where it will count automatic days when to wake up alarm by giving birthdate. And I have one comman alarm time for every scheduled date.

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



}

Broadcastreceiver...

       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(context, detail , 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" />

So My question is that My scheduler working perfectly. But after reboot device not a single alarm working. Help meee:))

like image 738
Utpal Ruparel Avatar asked Nov 24 '25 03:11

Utpal Ruparel


1 Answers

For this we need a Broadcast receiver that should receive "BOOT_COMPLETED" broadcast. We must know that when a device finishes booting Android System sends "BOOT_COMPLTED" broadcast.

Registering the BootReciever in android manifest file

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

and do not forget to add the following permission in manifest .

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

this permission is required to listen/reverie the BOOT_COMPLETED action

BootReceiver.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(); 

        }

   }

The onRecieve() method of BootReceiver will execute when boot completes, so wee need to write the code inside onreceive() method