2013-08-31 231 views
0

在我的應用程序中,我可以在指定的時間使用時間按鈕播放鬧鐘鈴聲。但是當我再次按設置的鬧鐘按鈕時,它會替換以前的鬧鐘..任何人都可以幫助存儲多個鬧鐘...並且還請告訴鬧鐘響鈴的時間存儲在應用程序中?在我的鬧鐘應用程序中設置多個鬧鐘

Alarmreceiver.java

package com.example.alaram; 

import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.PowerManager; 
import android.os.PowerManager.WakeLock; 
import android.util.Log; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 




      public class AlarmReceiver extends Activity{ 
     private MediaPlayer mPlayer; 
     private WakeLock mWakeLock; 
      Button stopalarm; 

     @SuppressWarnings("deprecation") 
     public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE); 
    mWakeLock =pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "my wakelock"); 
    mWakeLock.acquire(); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_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.alarmreceiver); 

    //Stop the alarm music 
    stopalarm=(Button) findViewById(R.id.btnStopoAlarm); 
    stopalarm.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mPlayer.stop(); 
       finish(); 
       return; 
     } 
    }); 
    PlaySound(this,getAlarmUri()); 
} 


private void PlaySound(Context context,Uri alert){ 
    mPlayer=new MediaPlayer(); 
    try{ 
     mPlayer.setDataSource(context,alert); 
     final AudioManager am=(AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     if(am.getStreamVolume(AudioManager.STREAM_ALARM)!=0); 
     { 
      mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); 
      mPlayer.prepare(); 
      mPlayer.start(); 
     } 

    }catch(IOException e) 
    { 
    Log.i("AlaramReciever", "no audio file"); 
    } 
} 

//Get an alarm sound. If none set, try notification, Otherwise, ringtone. 
private Uri getAlarmUri() 
{ 
    Uri alert= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    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(); 

} 

}

SetAlarm.java

package com.example.alaram; 

import java.util.Calendar; 
import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TimePicker; 
import android.widget.Toast; 

public class SetAlarm extends Activity { 

TimePicker timePicker; 
Button ok; 
int hrs,min; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.setalarm); 

//Operation of Ok button or Setting Alaram Time 
ok=(Button) findViewById(R.id.btnOk); 
ok.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     Intent intent=new Intent(SetAlarm.this,AlarmReceiver.class); 
     PendingIntent pi=PendingIntent.getActivity(SetAlarm.this, 2, intent,PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     timePicker=(TimePicker) findViewById(R.id.timePicker1); 
     hrs=timePicker.getCurrentHour(); 
     min=timePicker.getCurrentMinute(); 
     Calendar calendar=Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY,hrs); 
     calendar.set(Calendar.MINUTE, min); 
     calendar.set(Calendar.SECOND, 0); 

     alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi); 
     Toast.makeText(getBaseContext(), "Alaram is Set",Toast.LENGTH_SHORT).show(); 

     finish(); 
     } 

    }); 

} 

}

回答

3

這是因爲你總是使用相同的意向。如果您想要安排多個警報,則需要修改您的意圖。例如,您可以定義一個整數id並將每個新警報的值增加1。然後您可以使用下面的代碼將此id分配給intent

public class AlarmReceiver extends Activity { 

    private int id = 0; 

    @Override 
    public void onClick(View v) { 

     Intent intent=new Intent(SetAlarm.this,AlarmReceiver.class); 
     intent.setData(Uri.parse("alarm:" + (id++))); 

     ... rest of your code here 
    } 

} 

如果你想cancel()報警,你需要使用intent用的正是這種id

+0

我是新是java。你能告訴如何增加每個鬧鐘的ID嗎? – user2711295

+0

將'private int id = 0;'字段添加到您的類並添加'intent.setData()',如答案中所示。 –

+0

它仍然取代了報警:/ – user2711295

1

你是設置兩個報警器according to documentation相同的PendingIntent將會被替換:

如果已經有由filterEquals被定義爲這個意向計劃(有兩個意圖的 平等報警(意向) ),然後 它將被刪除並由此替換。

According to the documentation兩個目的是等於上filterEquals(Intent)這意味着基:

確定是否兩個目的是意圖 分辨率(濾波)的目的是相同的。也就是說,如果他們的行爲,數據,類型,類別, 和類別是相同的。這不會比較意圖中包含的任何額外數據 。

所以基本上如果你想安排兩個警報,只要確保你的意圖是不同的。

0

可以使用setRepeating這樣

alm.setRepeating(type, triggerAtMillis, intervalMillis, operation); 
+0

我想你誤會了我..我不想在某段時間內重複鬧鐘。我只是想存儲已經設置的所有報警而不被替換 – user2711295