现在很少有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();
}