在我的应用程序中,我可以使用时间选择器在指定时间播放闹钟铃声。但是当我再次按下设置闹钟按钮时,它会替换以前的闹钟。任何人都可以帮助存储多个闹钟...并且请告诉时间在哪里用于响铃的警报存储在应用程序中?
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();
}
});
}
}