0

I am trying to work with AlarmManager and service to send auto SMS to some number. I followed some tutorials and I wrote something like this:

        SwitchPreference testPref = (SwitchPreference) findPreference("key_auto_sms");
        SharedPreferences sharedPrefs = getActivity().getSharedPreferences("XXXXXXXXX", MODE_PRIVATE);
        testPref.setChecked(sharedPrefs.getBoolean("AutoSMS", false));

        testPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                if (testPref.isChecked()) {

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    int curHr = calendar.get(Calendar.HOUR_OF_DAY);
                    // Checking whether current hour is over 14
                    if (curHr >= 15)
                    {
                        // Since current hour is over 15, setting the date to the next day
                        calendar.add(Calendar.DATE, 1);
                    }


                    calendar.set(Calendar.HOUR_OF_DAY, 15);
                    calendar.set(Calendar.MINUTE, 1);
                    calendar.set(Calendar.SECOND, 00);


                    AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                    Intent i = new Intent(getActivity(), AlarmReceiver.class);
                    final PendingIntent pi = PendingIntent.getBroadcast(getActivity(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);


                    SharedPreferences.Editor editor = getActivity().getSharedPreferences("XXXXXXXXX", MODE_PRIVATE).edit();
                    editor.putBoolean("AutoSMS", true);
                    editor.commit();
                    Toast.makeText(getActivity(), getResources().getString(R.string.Auto_SMS_Enable), Toast.LENGTH_SHORT).show();


                } else {

                    SharedPreferences.Editor editor = getActivity().getSharedPreferences("com.camel.work_list", MODE_PRIVATE).edit();
                    editor.putBoolean("AutoSMS", false);
                    editor.commit();
                    Toast.makeText(getActivity(), getResources().getString(R.string.Auto_SMS_Cancle), Toast.LENGTH_SHORT).show();

                    Intent intent1 = new Intent(getActivity(), AlarmReceiver.class);
                    final PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager alarm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                    alarm.cancel(pendingIntent);
                    //getActivity().stopService(intent1);
                }
                return false;
            }
        });
    }

My BroadcastReceiver:

    public class AlarmReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
       // MY auto sms method.....
      }
    }

So the problem is that the alarm will work only one time and then the day after that it stops.

What am I missing?

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
sharon2469
  • 89
  • 1
  • 11

1 Answers1

1

You should not use setRepeating() because it's not accurate at time intervals. You can use the set() function at a time, it will work more accurately, when the broadcast is called you can call set() again (like recursive), it will work. This is my alarm set code, It will not work with some devices like huawei because the processes will be killed. With huawei I use JobScheduler. It works fine in case the screen is bright when the screen is off. It does not work correctly. Recently there is a WorkManager library that works better for all devices, I have not had the time to research it, so I do not give any advice on the WorkManager library. Hope it helps

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            assert alarmManager != null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(time, pendingIntent), pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
            }
Chanh
  • 433
  • 3
  • 12
  • I will try your method, But another question, Maybe you suggest to use some library from Github? – sharon2469 Aug 13 '18 at 08:24
  • 1
    link documentation documentation from google: https://developer.android.com/topic/libraries/architecture/workmanager if you want lib github, it's here: https://github.com/googlecodelabs/android-workmanager Note, I have not used or followed so I do not guarantee it will work well. – Chanh Aug 13 '18 at 09:55
  • Hey bro, I really really need your help, it doesn't work. I was try to do it in recursive with only "set()" and it work ones and that's it, But if i put in on setRepeating it's works for 3 days and after that nothing :(... What is wrong? – sharon2469 Aug 22 '18 at 06:59
  • I do not understand, can you post code to github and i can help, as i also recently worked with alarmmanager recently – Chanh Aug 22 '18 at 09:13
  • I upload part of the code: https://gist.github.com/sharon2469/7c517657afb4c1c8d368e3d56e6257fb – sharon2469 Aug 22 '18 at 11:12
  • OMG bro I think that i found the problem... look at Opiatefuchs answer, https://stackoverflow.com/questions/36328244/alarmmanager-not-working-properly – sharon2469 Aug 22 '18 at 11:42
  • I see a small problem in your code, since you use the add() function and then use the set() function for Calendar, I think that's the problem it does not repeat. You must call the add function after using the set() function to work – Chanh Aug 23 '18 at 01:42
  • And please use my statement above for different API versions instead of just using alarm.set (). – Chanh Aug 23 '18 at 01:43
  • Hey bro, it's works perfect :) But now i faced with another issue, The: ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS And The: ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS I do some research and i found this post: https://stackoverflow.com/questions/33114063/how-do-i-properly-fire-action-request-ignore-battery-optimizations-intent/33114136 If my app is send AUTO SMS in specific time every 24 hours do i allowed to add this permission ? – sharon2469 Aug 29 '18 at 07:53
  • I don't know, I have never used this permission. – Chanh Aug 30 '18 at 01:59