为什么在设定通知时间的时候提高剂量通知?

时间:2016-02-03 14:34:06

标签: java android time alarmmanager android-notifications

我已经使用时间选择器来设置用户通知的时间。我使用过警报管理器和通知构建器。

我在按钮的clickListener上调用了setAlarm方法来保存数据。因此,当调用此setAlarm方法时,Notification会同时引发,并且在用户设置时引发。

我不希望在保存数据时引发它。

有人能说出为什么会这样吗?

setAlarm方法

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

    intent.putExtra("title",eventTitle);


    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, targetmCalen.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, alarmIntent);

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

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

}

NotificationReceiver

public class NotificationReceiver  extends BroadcastReceiver {


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

    EventTableHelper db;

    @Override
    public void onReceive(Context context, Intent intent) {


        Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();

        db = new EventTableHelper(context);

        List<EventData> testSavings = db.getAllEvents();

        for (EventData ts : testSavings) {
            String log = "from date:" + ts.getFromDate()
                    + " ,to date: " + ts.getToDate()
                    + " ,location: " + ts.getLocation()
                    + " ,title " + ts.getTitle();

            Date date = new Date();
            Date date1 = new Date();
            Log.d("Result: ", log);

            SimpleDateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
            SimpleDateFormat df2 = new SimpleDateFormat("HH:mm a");

            try {
                date = df.parse(ts.getFromDate());
                date1 = df.parse(ts.getToDate());
            } catch (ParseException ex) {

            }
            String timeFrom = df2.format(date);
            String startTime = String.valueOf(timeFrom);

            String timeTo = df2.format(date1);
            String endTime = String.valueOf(timeTo);


            String location = ts.getLocation();
            String title = ts.getTitle();


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

            if(location.equals(""))
            {
                String msg = "From : " + startTime + "\nTo : " + endTime;

                myNotification = new NotificationCompat.Builder(context)
                        .setContentTitle("Event : " + title)
                        .setContentText(msg)
                        .setWhen(System.currentTimeMillis())
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setSmallIcon(R.drawable.eventicon)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .build();

            }

            else
            {
                String msg = "From : " + startTime + "\nTo : " + endTime + "\nAt : " + location;
                myNotification = new NotificationCompat.Builder(context)
                        .setContentTitle("Event : " + title)
                        .setContentText(msg)
                        .setWhen(System.currentTimeMillis())
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setSmallIcon(R.drawable.eventicon)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .build();

            }

            Log.i("Notify", "Notification");
            notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        }
    }
}

编辑:

Calendar object Passed in setAlarm method

     c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND,0);
            c.set(Calendar.MILLISECOND,0);
            c.set(Calendar.DATE, day);
            c.set(Calendar.DAY_OF_WEEK,2);
            notification = c.getTime();
            notificationTime = df.format(notification);
            Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();

谢谢..

0 个答案:

没有答案