Android:警报响起时如何在警报接收器内获取requestCode

时间:2014-09-27 14:51:19

标签: android android-intent

用户可以设置多个警报。在这种情况下,我将警报数据存储到数据库中并创建具有唯一PendingIntent的{​​{1}}。实际上,我使用数据库的行ID作为requestCode,当警报触发时,我可以从数据库中获取该特定警报的其他信息。

从我设定意图的地方开始:

requestCode

在我的闹钟接收器课程中,我想要将我设置为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(); } } 的行ID:

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(); } } 类中获得requestCode未决意图或行ID?

1 个答案:

答案 0 :(得分:6)

在创建意图时,将Extra添加到其中:

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);

在收件人中:

@Override
public void onReceive(Context pContext, Intent pIntent){
    // retrieve the value
    int code= pIntent.getIntExtra("requestCode", 1);

}