您的位置:首页 > 移动开发 > Android开发

Android:通知随记1

2015-12-14 22:27 471 查看
1)通知的分类
普通通知,大视图通知,进度通知,自定通知
2)获取通知管理者
NotificationManager notificationMananger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3)创建通知公共的一些参数:
公共的一些参数:
// 创建视图Builder对象
NotificationCompat.Builder builder = null;
builder = new NotificationCompat.Builder(this);// 创建通知Builder对象
// 通知的图片
builder.setSmallIcon(R.drawable.qq);
// 设置自动消失
builder.setAutoCancel(true);
// 获取声音的Uri
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ getPackageName() + "/" + R.raw.eatthings1);
// 默认的声音
// builder.setDefaults(Notification.DEFAULT_VIBRATE |
// Notification.DEFAULT_SOUND);
// 设置自定义通知的声音
builder.setSound(sound);
builder.setDefaults(Notification.DEFAULT_VIBRATE);
// 获取PendingIntent
Intent intent = new Intent(this, Activity00.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
// 设置未来的意图
builder.setContentIntent(pIntent);
Notification notification = null;
4)普通通知
// 普通通知
// 设置标题
builder.setContentTitle("花千骨");
// 设置内容
builder.setContentText("我神的名义诅咒你(白子画):生生世世,不伤,不老,不死");
notification = builder.build();
notificationMananger.notify(0, notification);
5)大视图通知
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(
builder);
style.setBigContentTitle("大视图通知");// 设置标题
for (int i = 0; i < datas.length; i++) {
style.addLine(datas[i]);// 一行一行加到箱子
}
// 创建通知对象
notification = builder.build();
// 短信管理者调用通知对象,进行通知
notificationMananger.notify(1, notification);
6)进度条通知
builder.setProgress(100, values[0], false);
7)自定通知
// 创建远程的View
// packageName:包名
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.view_notification_remote);
// 修改远程View
remoteViews.setTextViewText(R.id.remote_tv, "中秋节快乐");
remoteViews
.setImageViewResource(R.id.remote_image, R.drawable.sina);
builder.setContent(remoteViews);
notification = builder.build();
notificationMananger.notify(3, notification);
8)取消通知
notificationMananger.cancelAll();
cancel(id);

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: