Android Notification.setLatestEventInfo弃用替代和Notification.Builder用法

现在很少有4.0以下的手机,可以直接用新api  最小SDK由8改为16

        //n.setLatestEventInfo(this, label,getString(R.string.alarm_notify_snooze_text,Alarms.formatTime(this, c)), broadcast); //里面有四个参数,分别是context,标题,内容,pendingIntnet(即点击通知后调用的Intent)
        n = new Notification.Builder(this) //上下文
                .setAutoCancel(true)
                .setContentTitle(label) //标题
                .setContentText(getString(R.string.alarm_notify_snooze_text,Alarms.formatTime(this, c))) //内容
                .setContentIntent(broadcast) //点击通知后调用的Intent
                //.setSmallIcon(icon)
                .setWhen(System.currentTimeMillis())
                .build();
        nm.notify(mAlarm.id, n);

 

 

 

 

兼容 

private void snooze() {
        if (!findViewById(R.id.snooze).isEnabled()) {
            dismiss(false);
            return;
        }
        final String snooze =
                PreferenceManager.getDefaultSharedPreferences(this)
                .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
        int snoozeMinutes = Integer.parseInt(snooze);

        final long snoozeTime = System.currentTimeMillis()
                + (1000 * 60 * snoozeMinutes);
        Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id,
                snoozeTime);
        final Calendar c = Calendar.getInstance();
        c.setTimeInMillis(snoozeTime);
        String label = mAlarm.getLabelOrDefault(this);
        label = getString(R.string.alarm_notify_snooze_label, label);
        Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
        cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
        cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
        PendingIntent broadcast =
                PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
        NotificationManager nm = getNotificationManager();
        Notification n = new Notification(R.drawable.stat_notify_alarm,
                label, 0);
        n.flags |= Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_ONGOING_EVENT;
        
        
        
        
        //n.setLatestEventInfo(this, label,getString(R.string.alarm_notify_snooze_text,Alarms.formatTime(this, c)), broadcast);
        if (Build.VERSION.SDK_INT <16) {
            Class clazz = n.getClass();
            try {
                Method m2 = clazz.getDeclaredMethod("setLatestEventInfo", Context.class,CharSequence.class,CharSequence.class,PendingIntent.class);
                //setLatestEvent(xx)里面有四个参数,分别是context,标题,内容,pendingIntnet(即点击通知后调用的Intent)
                m2.invoke(this, label,getString(R.string.alarm_notify_snooze_text,Alarms.formatTime(this, c)), broadcast);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        else
        {
            n = new Notification.Builder(this)
                    .setAutoCancel(true)
                    .setContentTitle(label)
                    .setContentText(getString(R.string.alarm_notify_snooze_text,Alarms.formatTime(this, c)))
                    .setContentIntent(broadcast)
                    //.setSmallIcon(icon)
                    .setWhen(System.currentTimeMillis())
                    .build();
        }
        nm.notify(mAlarm.id, n);








        String displayTime = getString(R.string.alarm_alert_snooze_set,
                snoozeMinutes);
        Toast.makeText(AlarmAlertFullScreen.this, displayTime,
                Toast.LENGTH_LONG).show();
        stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
        finish();
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值