API16版本及以上的Notification用法
Intent intent = new Intent(App.getContext(), MainActivity.class);
intent.putExtra("kk", 1);
PendingIntent pend = PendingIntent.getActivity(App.getContext(),201,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap largeIcon = ((BitmapDrawable) App.getContext().getResources().getDrawable(R.drawable.icon101)).getBitmap();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN){
Notification notification = new Notification.Builder(App.getContext())
.setContentTitle("阈值")
.setContentText("车辆编号" + finalI + "余额" + balance + "阈值" + yuzhi)
.setContentIntent(pend)
.setSmallIcon(R.drawable.icon_1)
.setLargeIcon(largeIcon)
.setNumber(1)
.getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1,notification );
}
兼容新老API版本的Notification用法
Intent intent = new Intent(App.getContext(), MainActivity.class);
intent.putExtra("kk", 1);
PendingIntent pend = PendingIntent.getActivity(App.getContext(),201,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager nm = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(getContext());
builder.setSmallIcon(R.drawable.icon_1);
if (pend != null){
builder.setContentIntent(pend);
}
builder.setContentTitle("阈值");
builder.setContentText("车辆编号" + finalI + "余额" + balance + "阈值" + yuzhi);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.getNotification(); //builder.getNotification();重点
nm.notify(1024,notification);