在目前版本中用于实现通知的一个实例。
IDE版本:Android studio 2.2 beta
安卓版本:
虚拟机 nexus5 5.1.0
三星 galaxy 5.1.0
函数 DisOnNotification
功能 显示一则通知示例到通知栏上
返回值无
void DisOnNotification()
{
//获得通知栏管理器
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化一个通知,使用app的启动图标作为logo,通知名称为“提醒”
Notification notification = new Notification(R.mipmap.ic_launcher, "提醒", System.currentTimeMillis());
//这里的FirstpageActivity是我自定的页面名,按照需求修改
// Intent descIntent = new Intent(this, FirstpageActivity.class);
Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");
PendingIntent pendingIntent = PendingIntent.getActivity(FirstpageActivity.this, 1, intent, 0);
//-----配置通知栏界面
Notification.Builder builder = new Notification.Builder(FirstpageActivity.this);
builder.setAutoCancel(false);
builder.setTicker("Text");
builder.setContentTitle(getString(R.string.NotiContentTitle));
builder.setContentText("Set time is 1:30AM");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("已经坚持了0天"); //API level 16
// builder.setNumber(100);
builder.build();
//-----点击通知栏通知后通知自动消失
notification.flags|=Notification.FLAG_AUTO_CANCEL;
//用刚才配置的Builder通知栏通知赋值
notification= builder.getNotification();
manager.notify(11, notification);
// notification.defaults |= Notification.DEFAULT_SOUND;//默认声音提示
//发送通知,
// manager.notify(0, notification);
}
【新版本中似乎没有drawable而改为了mipmap存储图像】