java notification浮动通知 为什么在设定通知时间时通知提高了通知?



android通知 (0)

我用时间选择器来设置用户通知的时间。 我使用了警报管理器和通知生成器。

我在一个按钮的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();

谢谢..





android-notifications