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