locked
AlarmManager not working after device is rebooted RRS feed

  • Question

  • User166490 posted

    I am insert multiple Alarms ,but after mobile restart its not working.

    TodoActivity.cs

                     {
                Intent alarmIntent1 = new Intent(context, typeof(AlarmReceiver));
                      alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);   
                      var dtBasis = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 
                      Intent alarmIntent = new Intent(context, typeof(AlarmReceiver));
                      alarmIntent.PutExtra("title", "To Do");
                       alarmIntent.PutExtra("message", Notifi.Description.ToString()); 
                        var dateString = AlaramDateTime.ToString("hh:mm tt, dddd dd MMMM");
                      alarmIntent.PutExtra("date", dateString);
                      var dateString1 = AlaramGetDueDateTime.ToString("hh:mm tt, dddd dd MMMM");
                       alarmIntent.PutExtra("duedatetime", dateString1);
                       alarmIntent.PutExtra("AlarmID", Notifi.NotificationId.ToString());                                                             
                       PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, Notifi.NotificationId, alarmIntent, PendingIntentFlags.UpdateCurrent);
                        var firstTime = (long)Notifi.NotificationDateTime.ToUniversalTime().Subtract(dtBasis).TotalMilliseconds;
                        alarmManager.Set(AlarmType.RtcWakeup, firstTime, pendingIntent);
              }
    

    AlarmReceiver.cs

          [BroadcastReceiver]
            [IntentFilter(new[] { Intent.ActionBootCompleted })]
             public class AlarmReceiver : BroadcastReceiver
            {
                DateTime date, date1;
                private static Intent notIntent;
                public static List<LanguageCode> Language = new List<LanguageCode>();
                private static List<Timeformat> timeformat = new List<Timeformat>();
    
                public override void OnReceive(Context context, Intent intent)
                {
    
                    try
                    {
    
                        if ((intent.Action != null) && (intent.Action == Intent.ActionBootCompleted))
                        {
                            logger.Trace("ALARAM ActionBootCompleted");
                         // context.StartService(new Intent(context, typeof(AlarmService)));
    
                        }     
                        logger.Trace("ALARAM");
                        Context mContext = Android.App.Application.Context;
                        AppPreferences ap = new AppPreferences(mContext);
    
                        var message = intent.GetStringExtra("message");
                        var title = intent.GetStringExtra("title");
                        var MediID = intent.GetStringExtra("MediID");
                        var Time = intent.GetStringExtra("date");
                        var Alarmid = intent.GetStringExtra("AlarmID");
                        var dueTime = intent.GetStringExtra("duedatetime"); 
    
                            notIntent = new Intent(context, typeof(MainActivity));
                            id = Resource.Drawable.ic_blkreminder;
                            notIntent.PutExtra("Title", title);
                            notIntent.PutExtra("Message", message);
                            notIntent.PutExtra("AlarmTime", Time);
                            notIntent.PutExtra("MedicineAlarmID", Alarmid);
                            notIntent.PutExtra("DueDateTime", dueTime);
    
    
                        PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
                        Notification.Builder builder;
                        Notification.BigTextStyle textStyle = new Notification.BigTextStyle();
    
                            builder = new Notification.Builder(context)
                                      .SetSmallIcon(id)
                                      .SetContentTitle(alatit)
                                      .SetDefaults(NotificationDefaults.Sound)
                                      .SetContentText(message)
                                      .SetAutoCancel(true)
                                      .SetContentIntent(contentIntent);
    
    
                        builder.SetStyle(textStyle); 
                        builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone));
                        builder.SetLargeIcon(BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.SmdLogo));
    
                        Notification notification = builder.Build(); 
                        NotificationManager mNotificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); 
                        // Turn on vibrate:
                        notification.Flags |= NotificationFlags.Insistent; 
    
                        mNotificationManager.Notify(1, notification);
                    }
                    catch(Exception ex)
                    {
                        logger.Trace(ex.ToString());
                    }
                }
            }
    

    AndroidManifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Smart.Smart" android:versionCode="1" android:versionName="1.0" android:installLocation="internalOnly">
            <uses-sdk android:minSdkVersion="16" />
            <uses-permission android:name="android.permission.WAKE_LOCK" />
            <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
            <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
            <uses-permission android:name="android.permission.READ_CALENDAR" />
            <uses-permission android:name="android.permission.WRITE_CALENDAR" />
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
            <application 
            android:label="Smart"
            android:icon="@drawable/SMART"
            android:hardwareAccelerated="false"
            android:largeHeap="true">
            <!--<service android:name=".AlarmService"/>-->
             <!--<receiver android:name=".AlarmReceiver"></receiver>--> 
             <receiver android:name=".AlarmReceiver" android:enabled="true" >
             <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED" />
               <category android:name="android.intent.category.DEFAULT" />
               <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
            </receiver> 
            </application>
        </manifest>
    
    Saturday, February 17, 2018 6:59 AM

Answers

  • User84670 posted

    @AnbuMani27, alarms are gone after restarting the device, but there is an alternative.

    Create a BootReceiver for ActionBootCompleted extending WakefulBroadcastReceiver and in this Receiver you create all of your alarms again. In this case you will need to save the alarms on SQLite or something like this, so you can query them when the boot is completed.

    I suggest changing your AlarmReceiver to extend WakefulBroadcastReceiver as well.

    Please let me know if it helps you.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, February 18, 2018 11:24 AM

All replies

  • User84670 posted

    @AnbuMani27, alarms are gone after restarting the device, but there is an alternative.

    Create a BootReceiver for ActionBootCompleted extending WakefulBroadcastReceiver and in this Receiver you create all of your alarms again. In this case you will need to save the alarms on SQLite or something like this, so you can query them when the boot is completed.

    I suggest changing your AlarmReceiver to extend WakefulBroadcastReceiver as well.

    Please let me know if it helps you.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, February 18, 2018 11:24 AM
  • User166490 posted

    @Momm If i work on WakefulBroadcastReceiver method which means, no need to store any local DB Right ? Its work after mobile restart also ?

    Sunday, February 18, 2018 12:12 PM
  • User84670 posted

    No, you still need to store in a local DB.

    As for the Wakeful, now I noticed it got deprecated on Android 26, so it's better to use a BroadcastReceiver: - https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

    Sunday, February 18, 2018 1:30 PM
  • User166490 posted

    @Momm I am insert Multiple Alarms ... Any other ways :-(

    Sunday, February 18, 2018 4:07 PM
  • User84670 posted

    Not that I know, I had to do the same for more than 20 alarms...

    Sunday, February 18, 2018 8:21 PM
  • User166490 posted

    Can u help any sample for WakefulBroadcastReceiver ?

    Monday, February 19, 2018 4:13 AM