【问题标题】:How to set one time alarm in android如何在android中设置一次闹钟
【发布时间】:2013-03-28 08:57:12
【问题描述】:

我正在做警报项目,

我想设置一个时间闹钟..但是我面临设置tat的问题,

我的代码是;

public void loadCalender(String month) {

    try {

        Cursor cursor = null;

        Database db = new Database(getApplicationContext());

        cursor = db.getSelectedCalenderDetails(month);
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                do {
                    String text = cursor.getString(cursor
                            .getColumnIndex("event"));
                    String title = "News/Events";
                    String dates = cursor.getString(cursor
                            .getColumnIndex("date"));

                    String yr = dates.substring(0, 4);
                    int year = Integer.parseInt(yr);
                    String mon = dates.substring(5);
                    String mo = mon.substring(0, 2);
                    int months = Integer.parseInt(mo);
                    String da = dates.substring(9);
                    int day = Integer.parseInt(da);

                    // Ask our service to set an alarm for that date,
                    // this
                    // activity talks to the client that talks to the
                    // service
                    set_alarm(year, months, day, title, text);
                    System.out.println(dates);

                } while (cursor.moveToNext());

            }

        }
        cursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // looping through All Contacts

}


public void set_alarm(int year, int month, int day, String title,
        String text) {
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

    // etc
    Calendar cal = Calendar.getInstance();
    int hour = prefs.getInt("PREF_HOUR", 0);
    int min = prefs.getInt("PREF_MIN", 0);
    if(hour == 0 && min == 0){


    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.DAY_OF_MONTH, 7);

    cal.set(Calendar.HOUR_OF_DAY, 15);
    cal.set(Calendar.MINUTE, min);
    }else
    {

        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.DAY_OF_MONTH, hour);

        cal.set(Calendar.HOUR_OF_DAY, min);
        cal.set(Calendar.MINUTE, min);
    }
    Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
    intent.putExtra("title", title);
    intent.putExtra("text", text);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(), 1, intent,
            PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getApplicationContext()
            .getSystemService(getApplicationContext().ALARM_SERVICE);

    /*
     * alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
     * pendingIntent);
     */

    alarmManager.cancel(pendingIntent); // cancel any existing alarms

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,

            pendingIntent);
}

这里总是启用警报推送,当应用打开时...

我在代码中做错了什么.. 请帮我设置一次闹钟

提前致谢

【问题讨论】:

    标签: android push-notification alarms


    【解决方案1】:

    你需要使用setAlarm()方法而不是setRepeating()

    alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
    

    【讨论】:

    • 我还需要处理设备的启动吗?或者即使在设备重新启动后它仍然可以工作?如果应该调用它,但设备已关闭(例如没有电池),而现在设备已打开,该怎么办?
    • @androiddeveloper 你不必担心设备启动。 RTC 方法将警报设置为特定时间。因此,如果手机在那段时间内没有运行,则下次手机启动时将触发警报。
    • @Senhor 我明白了。时间变化的情况(例如“夏令时”)呢?如果我将它设置为 X 上的警报,但在 X 之前,国家将 X 更改为 X+1,在 X 到来之前。这意味着跳过了整整一个小时。它会对警报做什么?
    • @androiddeveloper 我相信警报会同时响起。 RTC 不是基于过去的时间。如果要处理此问题,则应改用 ELAPSED_TIME。但我不确定,我从未在你提到的情况下使用过 RTC。
    • @androiddeveloper 是 ?
    【解决方案2】:

    我用的怎么样

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    

    我想在以后通过 alarmMenager 推送通知 它在应用程序仍在运行时工作,而我调用 allarm menager 以在未来设置通知并关闭我的通知未显示的应用程序。 我该怎么办?

    这里你得到了我的事件发送者:

    Calendar cal = Calendar.getInstance();
     cal.set(Calendar.HOUR, HOUR_OF_DAY);
     cal.set(Calendar.MINUTE, MINUTE);
     //cal.add(Calendar.SECOND, SECOND_OF_DAY);
     Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class);
     intent.putExtra("alarm_status", statusMessage);
     intent.putExtra("alarm_title", title);
     intent.putExtra("alarm_content", content);
     Log.i("SenderEvent ", "przygotowane dane");
     PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    

    和接收者:

    Bundle bundle = intent.getExtras();
            String statusMessage = bundle.getString("alarm_status");
            String title = bundle.getString("alarm_title");
            String content = bundle.getString("alarm_content");
            nm = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                    new Intent(), 0);
    Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());;
            //notif.largeIcon = bitmap;
            notif.icon =2130837504;
            notif.tickerText=statusMessage;
            notif.when= System.currentTimeMillis(); 
            /*
            new Notification(0,
                    statusMessage, System.currentTimeMillis());*/
            notif.setLatestEventInfo(context, title, content, contentIntent);
            nm.notify(NOTIFY_ME_ID, notif);
    

    以后在应用关闭时推送通知有什么问题?

    【讨论】:

      【解决方案3】:

      不要使用alarmManager.setRepeating,而是使用alarmManager.set 来设置一次闹钟

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多