首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让我的闹钟在android里响1分钟?

如何让我的闹钟在android里响1分钟?
EN

Stack Overflow用户
提问于 2013-05-24 12:36:10
回答 2查看 2.2K关注 0票数 0

我需要在我的application.But上敲1分钟的闹钟,它只响一秒钟。

请给我建议。谢谢你宝贵的时间!

请找我的资料来源供参考。

AndroidAlarmService.java

代码语言:javascript
复制
public class AndroidAlarmService extends Activity {

private PendingIntent pendingIntent;
private static final int PERIOD=60000; // 1 minute

 /** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 Intent myIntent = new Intent(AndroidAlarmService.this, RepeatingAlarm.class);
 pendingIntent = PendingIntent.getBroadcast(AndroidAlarmService.this, 0, myIntent, 0);

 AlarmManager aM = (AlarmManager)getSystemService(ALARM_SERVICE);

 Calendar c=Calendar.getInstance();
 c.set(Calendar.HOUR, 6);
 c.set(Calendar.MINUTE, 0);
 c.set(Calendar.SECOND, 0);

  PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(getApplicationContext(),AndroidAlarmService.class),PendingIntent.FLAG_UPDATE_CURRENT);


 aM.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),PERIOD,pendingIntent);

    }
 }

RepeatingAlarm.java

代码语言:javascript
复制
 public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Toast.makeText(context,"Alarm Started.....", Toast.LENGTH_LONG).show();


        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();

        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(500);

        }
    }
EN

回答 2

Stack Overflow用户

发布于 2013-05-24 14:04:44

我想用铃声你不会成功的。这里有一个关于循环铃声的小帖子:

http://xanderx.com/2010/08/25/making-ringtones-loop-on-android/

但我还没试过。相反,您可以使用MediaPlayer循环并创建一个自定义倒计时器来停止mediaPlayer onFinished()。但这只是一个可能的解决办法..。

代码语言:javascript
复制
        try {

     Uri ringtoneAlert =  RingtoneManager.
        getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

         mRingtoneLooper = new MediaPlayer();
         mRingtoneLooper.setDataSource(this, ringtoneAlert);

     final AudioManager audioRingtoneManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

     if (audioRingtoneManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {

            mRingtoneLooper.setAudioStreamType(AudioManager.STREAM_RING);
            mRingtoneLooper.setLooping(true);
            mRingtoneLooper.prepare();
            mRingtoneLooper.start();

            //start custom countdown timer for 60 seconds, counts every second down
            //counting every second down is not necessary, You could even set every 5 seconds or whatever You want

            RingtoneStopper stopper = new RingToneStopper(60000,1000);
            stopper.start();
       }
     } catch(Exception e) {

      //do some message to user if some error occurs
   } 

     //define a countdown timer for 60 seconds that stops the MediaPlayer


        public class RingtoneStopper extends CountDownTimer{

             public RingtoneStopper(long millisInFuture, long countDownInterval) {
                  super(millisInFuture, countDownInterval);
              }

                   @Override
                     public void onFinish() {
                        mRingtoneLooper.stop();
                 }

                   @Override
             public void onTick(long millisUntilFinished) {
                  //need nothing to do on tick events
                 }
                }
票数 1
EN

Stack Overflow用户

发布于 2013-05-24 13:48:13

请检查这段代码,我想这会对你有帮助的。

代码语言:javascript
复制
Ringtone _ringtone =  RingtoneManager.getRingtone(context, notification);
Timer _start_ringtone = new Timer();
    while (_ringtone!=null) {
if(!_ringtone.isPlaying()){
        _ringtone.play();
}
    }
    _start_ringtone.schedule(new TimerTask() {
        @Override
        public void run() {
            _ringtone.stop();
_ringtone=null;
        }
    }, 60000);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16735145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档