I'm making and application in android using multiple alarms, and i want to do differents things for each alarm, my problem is that i don't know how to recover the id of the alarm or differentiate each alarm in alarm class. This is my code:
//Activate the alarm
public void ActivateAlarm(int num) {
int seconds =Preferences.getTime(num);
myIntent[num] = new Intent(Settings.this,
Alarm.class);
pendingIntent[num] = PendingIntent.getService(Settings.this, num,
myIntent[num], 0);
alarmManager[num] = (AlarmManager) getSystemService(ALARM_SERVICE);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager[num].setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
seconds * 1000, pendingIntent[num]);
Toast.makeText(Settings.this, "Alarm"(num+1)+"activated",Toast.LENGTH_LONG)
.show();
}
class Alarm
public class Alarm extends Service implements Runnable {
public int alarmID;
private static Thread thread;;
@Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(1);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Toast.makeText(Alarm.this, Alarm.thread.getName(), Toast.LENGTH_LONG).show();
//ReadFile.readFile(Integer.parseInt(thread.getName()));
displayNotification();
}
};
private void displayNotification() {
//different notification for each alarm
}
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
//super.onStart(intent, startId);
thread = new Thread(this);
thread.start();
}
public void setIDAlarm(int pos){
this.alarmID=pos;
}
}
Thank you.
SInce it worked for you i'll post as answer for others:
You can use
intent.setAction("MyAction1")
and then filter your alarms with
intent.getAction().equals("MyAction1") { do something ...}