每5分钟后如何点火

时间:2014-05-21 05:34:36

标签: android alarmmanager

我希望我的应用中的暂停功能会在每5分钟后触发警报 我正在制作一个应用程序,其中警报在用户选择的日期触发,对话框关闭2个按钮并且打盹关闭按钮工作正常但是打盹崩溃应用程序而不显示任何错误  这是我的代码:

Button snooze = (Button)findViewById(R.id.btn_snooze);
snooze.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Toast.makeText(context, "Alarm set", Toast.LENGTH_LONG).show();
                     mMediaPlayer.stop();
                     Calendar calendar=Calendar.getInstance();
                     Intent intent = new Intent();
                     intent.setAction("action_e");
                     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123, intent, 0);

                     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*60*1000, pendingIntent);

                     //DialogActivity.this.finish ();
                }
            }); 

在警报恢复类我只是打印一个tok消息,警报被触发 我在哪里做错了?

3 个答案:

答案 0 :(得分:0)

只需在贪睡按钮中添加以下代码即可。

snooze.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                finish();

                // meidaPlayer.stop();
                Calendar today = Calendar.getInstance();
                Calendar cal = (Calendar) today.clone();


                cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)+ 0);
                cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 5);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                setAlarm(cal);

            }
        });

}

并编写 setAlram()的方法,如下所示。

private void setAlarm(Calendar targetCal) {

        if (targetCal.get(Calendar.HOUR) > 12)
            hours = targetCal.get(Calendar.HOUR) - 12;
        else
            hours = targetCal.get(Calendar.HOUR);

        Toast.makeText(getApplicationContext(),
                "Alaram set to" + hours + ":" + targetCal.get(Calendar.MINUTE),
                Toast.LENGTH_LONG).show();

        // String time_selected = hours + ":" + targetCal.get(Calendar.MINUTE);

        if (targetCal.get(Calendar.HOUR_OF_DAY) > 0) {

            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    targetCal.getTimeInMillis(), pendingIntent);
        }

    }

希望它会帮助你。可能知道你的反馈。

答案 1 :(得分:0)

像这样使用:

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + 5*60*1000,alarmManager.INTERVAL_DAY, pendingIntent);

答案 2 :(得分:0)

在贪睡onclick()方法中编写此代码,我希望它能为您提供帮助

Handler handler = new Handler();

handler.postDelayed(new Runnable() {

    public void run() {

//play the alarm here

        }
        }, 5000);