user6548194 user6548194 - 4 months ago 27
Java Question

Save variable with alarm manager event

I'm building a study planner application for Android, i have the

AlarmManager
set up and working to trigger a notification when the time elapses, which works fine.
When setting the alarm, the alarm details are saved into an
SQLite
database, when the alarm is set off, i need to recall the alarm information, is there some form of variable i can save into the database and link to the specific alarm so that i can use it in an
SQLite query
to find the correct entry.

UPDATE Code below:

Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nId);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);

AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);


AlarmReciever:

public void onReceive(Context context, Intent intent)
{
long dbId = intent.getLongExtra("nID", 0);
String notificationId = Long.toString(dbId);
Log.i("App", "called receiver method");
try{
Toast.makeText(context, notificationId, Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}


Thanks in advance

Answer

You're sending a String through the intent, it needs a Long, this code should work.

Long nIdLong = System.currentTimeMillis();
        String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
                alarmIntent.putExtra("nID", nIdLong);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,  alarmIntent , 0);

                AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);