3

I am trying this code to popup the alarm message. Its working when launching or opening the app, but it doesn't say any popup message while outside of the app. I am so confused, i don't know what i am doing wrong.

String alarmtime = cur.getString(cur.getColumnIndex(DBDATA.LG_ALARMTIME));
//Reminder
String[] timesplit = alarmtime.split(":"); 
int hour = Integer.parseInt(timesplit[0]);
int minute = Integer.parseInt(timesplit[1]);
System.out.println(hour);
System.out.println(minute); 

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ShortTimeEntryReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 

Calendar alarm = new GregorianCalendar();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, minute); 
alarm.set(Calendar.SECOND, 0);  
System.out.println(System.currentTimeMillis());
System.out.println(alarm.getTimeInMillis());
if (System.currentTimeMillis() > alarm.getTimeInMillis()){
    alarm.setTimeInMillis(alarm.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
    alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
else
{
    alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);                 
}

I need to pop up the alarm message outside of the app(i.e) exactly like the alarm does. Thanks for your help guys,

Itai Bar-Haim
  • 1,686
  • 16
  • 40
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • I think you'll need Services. http://developer.android.com/reference/android/app/Service.html – MikkoP May 22 '12 at 12:36

1 Answers1

2

You probably need a BroadcastReceiver.

As you can read in this question : BroadcastReceiver not receiving an alarm's broadcast

You have to build the intent like this :

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, "CHECK_ALARM_CODE", alarmIntent, 0);

And receive the alarm like this :

    public class AlarmReceiver extends BroadcastReceiver{
      @Override
      public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
         Log.d("OK", "AlarmReceiver.onReceive");
      }
}

Don't forget to register your broadcast in your manifest file.

Community
  • 1
  • 1
Timothée Jeannin
  • 9,652
  • 2
  • 56
  • 65
  • Thanks for your comment.. Actually my alarm is working when i am inside of the app.. But its not when i am in other app.. – vinothp May 22 '12 at 12:46
  • I understand. But you want your alarm to be triggered when your application is closed right ? Then you have no activity running, no service running. All you can do is "intercept" the alarm intent with a broadcast receiver and then do whatever you want from that broadcast receiver (starting another activity for example ... ) – Timothée Jeannin May 22 '12 at 13:01
  • Did you solve your problem or do you need more explanations ? – Timothée Jeannin May 22 '12 at 13:22
  • Thanks for you explanation... I am already doing my job with the broadcast receiver.. I don't know all of sudden it stops working and now its starts working.... But your explanation is right.. i am just marking as answer.. thanks – vinothp May 22 '12 at 13:39
  • Thanks. :) Feel free to ask if you need more explanations. – Timothée Jeannin May 22 '12 at 13:48
  • @Timothee.. I had one problem.. While alaram dialog box get popuped, in back i am seeing the activity last used.. How to remove that.. I tried a lot but couldn't get solution.. – vinothp May 29 '12 at 12:50