1

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

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

    });

}   

}

4

3 回答 3

3

这是因为您总是使用相同的 Intent。如果您想安排多个警报,则需要修改您的意图。例如,您可以定义一个整数id并将每个新警报的整数加一。然后您可以使用下面的代码将其分配idintent.

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()报警,你需要使用intentthis id

于 2013-08-31T06:22:55.740 回答
1

根据将被替换的文档,您正在为两个警报设置相同的 PendingIntent :

如果这个 Intent 调度已经有一个警报(两个 Intent 的相等性由 filterEquals(Intent) 定义),那么它将被删除并替换为这个。

根据文档,两个意图是平等的,filterEquals(Intent)这意味着:

出于意图解析(过滤)的目的,确定两个意图是否相同。也就是说,如果它们的动作、数据、类型、类和类别相同。这不会比较意图中包含的任何额外数据。

所以基本上如果你想安排两个警报,只需确保你的意图是不同的。

于 2013-08-31T06:23:30.403 回答
0

你可以setRepeating这样使用

alm.setRepeating(type, triggerAtMillis, intervalMillis, operation);
于 2013-08-31T06:28:32.640 回答