2014-09-27 92 views
3

用戶可以設置多個鬧鈴。在這種情況下,我將警報數據存儲到數據庫中,並創建一個帶有獨特requestCodePendingIntent。其實我使用數據庫的行ID作爲requestCode,當報警觸發時,我可以得到該特定報警從數據庫中提取數據的其他信息。Android:鬧鈴響鈴時如何獲取鬧鈴中的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(); 
    } 
} 

在我報警接收器類我想我已經設置爲requestCode該行ID:

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待定意圖或此AlarmReceiverActivity類中的行ID?

+0

你的意思是POS機的情況下,你的價值? – OnePunchMan 2014-09-27 14:55:22

+0

@kaze:是的,我需要這個值。 – rony36 2014-09-27 14:58:54

回答

5

在創建意圖,把附加給它:

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

} 
+0

這是否解決您的問題? – OnePunchMan 2014-09-27 15:04:55

+0

無法解析方法'getExtra' :( – rony36 2014-09-27 15:05:47

+0

@ rony36檢查編輯。如果在此之後發現任何問題,請寫出問題。 – OnePunchMan 2014-09-27 15:14:49

相關問題