I have a Date array which stored dates and time in its respective positions in my app like:
private Date m_DateTimeArray[];
m_DateTimeArray = new Date[Utilities.DATE_TIME_MAX_COUNT];
m_DateTimeArray[0] = 1-july-2012 12:30:00
m_DateTimeArray[1] = 1-july-2012 01:00:00
m_DateTimeArray[2] = 1-july-2012 02:30:00
m_DateTimeArray[3] = 1-july-2012 03:15:00
m_DateTimeArray[4] = 1-july-2012 04:20:00
m_DateTimeArray[5] = 1-july-2012 05:00:00
m_DateTimeArray[6] = 1-july-2012 06:30:00
Now I exit my app, so now on the background what I want is, whenever my device current date matches this date stored in an array, it pops up one dialog with some information, similarly whenever my current date matches next date store in an array it again pop up a dialog. So this whole process should run whether my app is in a running state or not in a running state. So how that can be done. I go through many examples like AlarmManager, IntetService, Service, But not able to get the result that I want. So if anyone works on such issue, please help me to solve this out.
Code of my main Activity
public class AndroidAlarmService extends Activity
{
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button)findViewById(R.id.startalarm);
Button buttonCancel = (Button)findViewById(R.id.cancelalarm);
buttonStart.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 20);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
}});
buttonCancel.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(AndroidAlarmService.this, "Cancel!", Toast.LENGTH_LONG).show();
}});
}
}
Code in my alarm service
public class MyAlarmService extends IntentService
{
public void onCreate()
{
super.onCreate();
}
public MyAlarmService()
{
super("MyAlarmService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, startId, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
protected void onHandleIntent(Intent intent)
{
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
}
}
There are 2 ways like Making one BroadCast Receiver with the Action TIME_TICK and Using AlarmManager, But as reading your requirement there is only one preffered way for you is to use AlarmManager.
Whenever the data in the DATE Array generated you need to set one Alarm for each of the elements of the Array.
I am sure you have tried many Examples available on net for the same, Then I tell you to use them again EXAMPLE you will use this example as it is, you will just have to change one thing in the code like where they haved added
cal.add(Seconds,10);there instead of it you will have to add the difference of your current time and date array element time.The rest of the code from example will remain as it is.
EDIT Trying To Write Code