Reputation: 3339
User able to set multiple alarms. In this case I'm storing alarm data into database and creating a PendingIntent
with unique requestCode
. Actually I'm using row id of the database as requestCode
that when Alarm fires I can get other information of that specific alarm pulling data from database.
From where I'm setting intent:
private void setInstantAlarm(Calendar timeFromNow, int pos){
try{
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, pos, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeFromNow.getTimeInMillis(), pendingIntent);
}catch (NumberFormatException e){
Toast.makeText(context, "Something went wrong!", Toast.LENGTH_SHORT).show();
}
}
In my Alarm receiver class I want that row id which I've set as requestCode
:
public class AlarmReceiverActivity extends Activity {
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock mWakeLock;
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Wake Log");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.alarm);
Button stopAlarm = (Button) findViewById(R.id.btnStopAlarm);
stopAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.stop();
finish();
}
});
playSound(this, getAlarmUri());
}
private void playSound(Context context, Uri alert){
mMediaPlayer = new MediaPlayer();
try{
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0){
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}catch (IOException e){
Log.i("AlarmReceiver", "No audio files are Found!");
}
}
private Uri getAlarmUri(){
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
protected void onStop(){
super.onStop();
mWakeLock.release();
}
}
So how could I get that requestCode
of pending intent or the row id inside this AlarmReceiverActivity
class?
Upvotes: 3
Views: 3663
Reputation: 748
While creating the intent, put Extra to it:
Intent intent = new Intent(context, AlarmReceiverActivity.class);
intent.putExtra("requestCode",pos);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, pos, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeFromNow.getTimeInMillis(), pendingIntent);
in the receiver:
@Override
public void onReceive(Context pContext, Intent pIntent){
// retrieve the value
int code= pIntent.getIntExtra("requestCode", 1);
}
Upvotes: 8