Android笔记(二十八)通知的使用
来源:互联网 发布:linkin 大数据 编辑:程序博客网 时间:2024/05/22 03:21
一、通知的用法
当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。相比于广播接收器和服务,在活动里创建通知的场景还是比较少的,因为一般只有当程序进入到后台的时候我们才需要使用通知。
- 获得NotificationManager 的实例
- 创建一个 Notification 对象
- 设定通知的布局
- 调用 NotificationManager 的 notify()方法
二、具体实例——通过点击按钮来发出一条通知
- 建立布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_notice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send notice" /></LinearLayout>
- MainActivity
public class MainActivity extends ActionBarActivity { private Button button; private NotificationManager manager; private Notification.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.send_notice); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); builder = new Notification.Builder(MainActivity.this); Intent intent = new Intent(MainActivity.this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); builder.setContentIntent(contentIntent); builder.setTicker("这是一个通知"); builder.setContentTitle("通知"); builder.setContentText("hello"); builder.setDefaults(Notification.DEFAULT_ALL); builder.setSmallIcon(R.drawable.ic_launcher); Notification notification = builder.build(); manager.notify(1, notification); } }); } }
0 0
- Android笔记(二十八)通知的使用
- swiper的基础使用(二十八)
- Android文档笔记:通知(二)
- Android文档笔记:通知(二)
- Android之手机通知栏Notification的使用(二)
- 学习笔记(二十八)
- Android学习记录(二十八)--Android apache httpclients的使用。
- android 学习笔记 通知的使用
- Android的ExpandableListView-android学习之旅(二十八)
- OllyDbg 使用笔记 (十八)
- 软件工程视频学习笔记(二十八)
- JAVA学习笔记(二十八)
- python 学习笔记(二十八)
- javaScript笔记(二十八) JSON
- tensorflow学习笔记(二十八):collection
- java学习笔记(二十八)异常
- 信息安全工程师 学习笔记(二十八)
- android通知的使用
- 详解ORACLE数据库的分区表
- Linux重启中init6与reboot的区别
- Android动画-TweenAnimation
- 试验-探索自动化病毒样本动态行为分析系统中的漏洞
- 微软编译器vs 编译器(cl.exe)和连接器参数
- Android笔记(二十八)通知的使用
- [加密]在AES的CBC模式下 pydes vs crypto
- 【Android基础】——AsyncTask
- 观察者模式
- s2121b_16t 触摸按键 (君正)
- QtPropertybrowser源码编译
- JavaScript学习笔记8-jQuery简介、jQuery使用详解、DOM对象与jQuery对象的转换与区别
- hessian入门与springMVC框架集成---Service服务
- 如何解决Contiki下进程与中断时序Bug