通知剂量不会在选定的时间发生

时间:2016-02-01 17:39:49

标签: java android set android-notifications android-timepicker

我想在用户选择时间时设置通知。所以我添加了时间选择器对话框。我用日历设定了时间。

但是我没有收到任何通知,虽然我已经设定了时间。我有3个时间选择器对话框,因为我正在设置事件的开始时间和结束时间,现在我想设置通知时间。

当我设置通知时间时,在结束时间设置相同的时间。

这是设定时间的代码:

 @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {

        case TIME_DIALOG_ID:

            // set time picker as current time
            return new TimePickerDialog(this, timePickerListener, hour, minute,
                    false);

        case TIME_DIALOG:

            // set time picker as current time

            return new TimePickerDialog(this, timePickerListener, hour, minute,
                    false);

        case TIME:

            return new TimePickerDialog(this, timePickerListener, hour, minute,
                    false);
    }
    return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
        // TODO Auto-generated method stub
        hour = hourOfDay;
        minute = minutes;

        updateTime(hour, minute);

    }

};


// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hour, int minute) {

    df = new SimpleDateFormat("HH:mm a");

    if (timeType == 0) {

        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.DATE, day);

        datefrom = c.getTime();
        startTime = String.valueOf(datefrom);
        aTime = df.format(datefrom.getTime());
        showFromTime.setText(aTime);

        Toast.makeText(getApplicationContext(), String.valueOf(datefrom), Toast.LENGTH_LONG).show();

    } else if (timeType == 1) {

        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.DATE, day);

        dateto = c.getTime();
        endTime = String.valueOf(dateto);
        bTime = df.format(dateto.getTime());
        showToTime.setText(bTime);

        Toast.makeText(getApplicationContext(), String.valueOf(dateto), Toast.LENGTH_LONG).show();

    }
    else
    {
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.DATE, day);
        notification = c.getTime();
        notificationTime = df.format(notification);
        Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();

    }
}

通知服务类

    public class NotificationReceiver  extends BroadcastReceiver {


    private static final int MY_NOTIFICATION_ID = 0;
    NotificationManager notificationManager;
    Notification myNotification;

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();

        Intent myIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        myNotification = new NotificationCompat.Builder(context)
                .setContentTitle("Event Time")
                .setContentText("Time")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_alarm_black_48dp)
                .build();

        notificationManager =
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    }

}

编辑:设置警报功能

  @SuppressLint("NewApi")
private void setAlarm(Calendar targetmCalen) {

    AlarmManager alarmMgr;
    PendingIntent alarmIntent;
    alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
    Toast.makeText(getBaseContext(),
            "call alarmManager.setExact()",
            Toast.LENGTH_LONG).show();
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,targetmCalen.getTimeInMillis(),
            1000 * 60 * 20, alarmIntent);

    ComponentName receiver = new ComponentName(getApplicationContext(),NotificationReceiver.class);
    PackageManager pm = getApplicationContext().getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);


    //  Toast.makeText(getApplicationContext(),"Notification Set",Toast.LENGTH_SHORT).show();
}

请帮助..

0 个答案:

没有答案