在我报警的应用程序设置多个闹钟 [英] Setting Multiple alarms on my alarm application

查看:186
本文介绍了在我报警的应用程序设置多个闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我能够使用timepicker..But在指定时间播放警报声时,我preSS设定的报警按钮重新它取代了previous报警..任何人都可以帮助存储多个报警。 ..和也请告诉哪里振铃的报警时间存储在应用程序?

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

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

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

    });

}   

}

推荐答案

这是因为你总是使用相同的意向。如果你想有一个以上的报警预定,您需要修改你的意图。例如,您可以定义一个整数 ID 和一个增加它为每一个新的报警。然后你可以使用下面的code转让本 ID 意图

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
    }

}

如果你想取消()报警,你需要使用意图用的正是这种 ID

If you want to cancel() alarm, you need to use intent with exactly this id.

这篇关于在我报警的应用程序设置多个闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆