1

I wrote the following code to make a broadcast start service every day at 9.0 and 3.30. yet it always run only once. please let me know what have I done wrong. thank you in advance. I used preferences to check if the alarm is already set or not and then it skips. this is to avoid application to set alarm every time this runs.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        @SuppressWarnings("unused")
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        pref2 = getSharedPreferences(PREFS_N, MODE_PRIVATE);
        hh = pref2.getString(PREFS_USER, Da);

        if (hh.equals(Da)) {
            new update().execute();
        } else {
            Log.i("ssssssssssssssssss", hh);

            dates = (TextView) findViewById(R.id.date);
            buy = (TextView) findViewById(R.id.fbuying_rate);
            sell = (TextView) findViewById(R.id.fselling_rate);
            Ebuy = (TextView) findViewById(R.id.ebuying_rate);
            Esell = (TextView) findViewById(R.id.eselling_rate);

            buy.setText(pref2.getString(PREFS_FBUY, ""));
            sell.setText(pref2.getString(PREFS_FSELL, ""));
            Ebuy.setText(pref2.getString(PREFS_EBUY, ""));
            Esell.setText(pref2.getString(PREFS_ESELL, ""));
            dates.setText(pref2.getString(PREFS_DATE, ""));

        }
        pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        kk = pref.getString(PREFS_USERNAME, De);

        Log.i("gasf", kk);
        if (kk.equals(De))

        {

            Calendar updateTime = Calendar.getInstance();
            updateTime.set(Calendar.HOUR_OF_DAY, 9);
            updateTime.set(Calendar.MINUTE, 30);

            Intent alarm = new Intent(this, AlarmReceiver.class);
            int i = 23;
            AI = PendingIntent.getBroadcast(this, i, alarm, PendingIntent.FLAG_CANCEL_CURRENT);
            am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, AI);


            Calendar updateTime2 = Calendar.getInstance();
            updateTime2.set(Calendar.HOUR_OF_DAY, 15);
            updateTime2.set(Calendar.MINUTE, 30);

            Intent alarm2 = new Intent(this, AlarmReceiver.class);
            int i2 = 32;
            AI2 = PendingIntent.getBroadcast(this, i2, alarm2, PendingIntent.FLAG_CANCEL_CURRENT);
            am2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            am2.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, AI2);


            getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(PREFS_USERNAME, "lol").commit();
        }
        lvCustomList = (ListView) findViewById(R.id.lv_custom_list);
        showList();

    }
kapilgm
  • 1,620
  • 1
  • 15
  • 22
checkmate
  • 267
  • 2
  • 6
  • 19

1 Answers1

1

am2.setInexactRepeating is the thing! Isn't that self explainatory? You need setExact() / setRepeating() (Depends on your use case) - For more information check my other answer here

setInexactRepeating, is OS and battery friendly, it batches together all the work to be done on Alarm receive and works through one by one, while as setRepeating instantly fires the alarm Also a note: Alarms are wiped off once phone is rebooted, you might have to implement a boot broadcast receiver to make it persistent. Make sure you dont do that runtime, you need to implement it in the Manifest else when your app is not in background you will not receive any broadcasts.

Community
  • 1
  • 1
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • hey, I did try the same code with setRepeating() and as well, AI = PendingIntent.getBroadcast(this, i, alarm, PendingIntent.FLAG_UPDATE_CURRENT); still it did not work for me – checkmate Dec 04 '15 at 06:45
  • Please refer to the docs on how alarms work on and above API 19. You need manage this manually. Also provide some insight on your test environment. – Skynet Dec 04 '15 at 06:47
  • I am testing on API 17. target it set to api 23. min api 8. As you can see in my code, do you think i have written anything wrong? – checkmate Dec 04 '15 at 06:54
  • Please check against the code I have linked in my answer - That is production level code you can use. Also update your code in the question to reflect the changes you have made. – Skynet Dec 04 '15 at 06:57