问题
In my application I am able to play alarm tone on specified time using timepicker..But when i press the set alarm button again it replaces the previous alarm.. Could anyone help to store multiple alarms...and also please tell where the time for ringing the alarm is stored in application?
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();
}
});
}
}
回答1:
This is because you always use the same Intent. If you want to have more than one alarm scheduled, you need to modify your intent. For instance you can define an integer id
and increase it for every new alarm by one. Then you can use the code below to assign this id
to the 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
}
}
If you want to cancel()
alarm, you need to use intent
with exactly this id
.
回答2:
You are settings the same PendingIntent for both alarms according to documentation it will be replaced:
If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
According to the documentation two intents are equals base on filterEquals(Intent)
which mean:
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
So basically if you want to schedule two alarms just make sure that your Intents are distinct.
回答3:
you can use setRepeating
like this
alm.setRepeating(type, triggerAtMillis, intervalMillis, operation);
来源:https://stackoverflow.com/questions/18545115/setting-multiple-alarms-on-my-alarm-application