5

我正在做警报项目,

我想设置一个时间警报..但我面临设置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);
}

此处警报推送始终启用,当应用程序打开时...

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

提前致谢

4

3 回答 3

10

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

alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
于 2013-03-28T09:00:35.137 回答
6

我用的怎么样

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

将来在应用程序关闭时推送通知有什么问题?

于 2013-06-10T11:01:02.357 回答
4

而不是使用 alarmManager.setRepeating 使用 alarmManager.set 设置一个时间警报

于 2013-03-28T08:59:29.313 回答