设为首页 加入收藏

TOP

安卓状态栏通知Status Bar Notification
2017-10-11 17:06:13 】 浏览:6636
Tags:安卓 状态 通知 Status Bar Notification

安卓系统通知用户三种方式:

1.Toast Notification

2.Dialog Notification

3.Status Bar Notification Status Bar Notification,状态栏通知

发送一个状态栏通知必须用到两个类:NotificationManager,Notification

1.NotificationManager是一个系统Service,必须通过getSystemService()获取

NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2.Notification是具体的状态栏通知对象

调用NotificationManager的notify()方法创建Notification

两部分:

①:状态栏通知    

notification.icon=R.drawable.ic_launcher;     

notification.tickerText="My First Notification";     

notification.when=System.currentTimeMillis();

②:下拉通知列表和点击跳转:

两种方式:

一:setLatestEventInfo()方法     

Context context = getApplicationContext();     

CharSequence contentTitle="Notification";     

CharSequence contentText="Notification Context";     

Intent intent=new Intent(Main.this,Turn.class);    

PendingIntent pendingIntent=PendingIntent.getActivity(Main.this, 0, intent, 0);     

notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);

二:自定义通知栏

notification.flags=Notification.FLAG_AUTO_CANCEL;用户点击后通知自动取消

设置两个变量contentView和contentIntent     

RemoteViews contenView=new RemoteViews(getPackageName(), R.layout.notification_layout);     

contenView.setImageViewResource(R.id.icon, R.drawable.ic_launcher);     

contenView.setTextViewText(R.id.contentText, "自定义通知");     

notification.contentView=contenView;          

Intent intent1=new Intent(Main.this,Turn.class);     

PendingIntent pendingIntent1=PendingIntent.getActivity(Main.this, 0, intent1, 0);     

notification.contentIntent=pendingIntent1;

Tips:

可能遇到的错误:Couldn't expand RemoteViews for:

检查是否是RemoteViews对应的layout里使用了它不支持的组件

检查RemoteViews对应的layout布局文件是否有基本错误,例如忘记声明宽高等

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android SQLite API的使用(非原.. 下一篇Android 自定义 View 浅析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目