android:am pm格式的报警

时间:2011-12-19 07:29:05

标签: android

我正在使用alarmmanger类设置方法来设置闹钟。一世  从用户通过timepicker对话框获取输入以进行设置  当我设置特定的小时和分钟时,alarm.alarm正常工作但是  应用程序不遵循am / pm逻辑。是指我设置闹钟  上午10点31分,而系统时间是晚上10:30,然后在晚上10点31分发出警报,而不是在上午10点30分。有人告诉我这是什么原因?

Calendar time = Calendar.getInstance();
        time.set(Calendar.HOUR, hourOfDay);
        time.set(Calendar.MINUTE, minute);
        time.set(Calendar.SECOND, 5);
        //time.set(Calendar.AM_PM);



        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        alarmMgr.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(), pendingIntent);

3 个答案:

答案 0 :(得分:1)

您需要使用SimpleDateFormat或DateFormat类以时间格式设置时间,该类将根据日期时间字符串给出日期,并使用getTime获取该值并在set方法中设置为警报。

或者您可以采取其他方式获取闹钟时间并检查其是上午还是下午,并将此属性设置为包含时间和日期的日历对象并获取毫秒值从日历设置到alarm.set()方法。

希望你能得到一些想法/建议

修改

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); // this is you format
Date d = sdf.parse("19/12/2011 03:47:00 pm"); // this string getting you from your timepicker
Intent myIntent = new Intent(youractivity.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(youractivity.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, d.getTime(), pendingIntent);

注意:你需要放入try和catch块解析部分,这可能会在解析时间内导致错误

答案 1 :(得分:0)

我在设置闹钟时使用了这种方法:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        public final static int MAX_SIZE = 100;
        public final static int MIN_SIZE = 10;
        private Rectangle bounds;
        private Random rand;

        public TestPane() {
            rand = new Random();
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1);

                    int randomPositionX = rand.nextInt(getWidth() - randomSize);
                    int randomPositionY = rand.nextInt(getHeight() - randomSize);

                    bounds = new Rectangle(randomPositionX, randomPositionY, randomSize, randomSize);
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (bounds != null) {
                g2d.setColor(Color.RED);
                g2d.fillOval(bounds.x, bounds.y, bounds.width, bounds.height);
            }
            g2d.dispose();
        }

    }

}

对于时区参数,请使用am = /** * Set the Alarm * * @param context the activity context * @param id the alarm ID for this app * @param hour the alarm hour * @param minute the alarm minute * @param timeZone the timezone am = Calendar.AM or pm = Calendar.PM */ public static void setAlarm(Context context, int id, int hour, int minute, int timeZone) { AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.AM_PM, timeZone); Intent intent = new Intent(context, PopupActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, id, intent, 0); alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 10, pIntent); } 或pm = Calendar.AM

答案 2 :(得分:0)

更改以下行:

time.set(Calendar.HOUR, hourOfDay);

time.set(Calendar.HOUR_OF_DAY, hourOfDay);