Android: how to get requestCode inside alarm receiver when alarm clock

The user can set multiple alarms. In this case, I store the alarm data in a database and create one PendingIntent

with a unique one requestCode

. In fact I am using the database row id as requestCode

that when an alarm occurs I can get other information about that particular alarm pulling data from the database.

Where I am setting the 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 this string id, which I 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 can I get the requestCode

pending intent or string id inside this class AlarmReceiverActivity

?

+3


source to share


1 answer


When creating an intent, add 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);

}

      

+5


source







All Articles