Android基础教程之Button事件发送消息到通知栏Notification
2015-06-19 08:30
609 查看
在手机上,我们经常会看到手机顶部会有收到消息,觉得还蛮不错的,于是乎今天研究了一下,做了点总结。 一、Notification通知栏通知 Notification是显示在手机状态栏的消息(手机状态栏位于手机最顶端),代表一种全局效果的通知。 二、通知栏的内容 一般包含图标、标题、内容、时间、点击后响应 三、如何实现通知栏 1.通知管理类 获取NotificationManager 显示通知栏:notify(id, notification); 取消通知栏:cancle(id); 2.通知类 构造Notification并设置显示内容 通知栏通知可以设置声音提示、指示灯,以及震动效果 步骤: 一、實現 OnClickListener接口public class MainActivity extends Activity implements OnClickListener{ 二、构造notification并发送到通知栏
这里特别讲一下,在开发中我们还需要用到pendingIntent类
关于这个类,我在网上做了一下大概的总结 首先,pendingIntent字面意义:等待的,未决定的Intent。 要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。 参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。 pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。 主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等 好了基础都讲的差不多了,现在演示一下普通的发送消息到通知栏,跟取消通知栏的操作。先上效果图:
布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jp.logdemo.MainActivity" tools:ignore="MergeRootFrame" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/toast_common" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="普通Toast" android:textSize="15sp" /> <Button android:id="@+id/toast_seat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="位置偏移Toast" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/toast_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="带图片的Toast" android:textSize="15sp" /> <Button android:id="@+id/toast_div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="自定义的Toast" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alertdialog_sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="确认对话框" android:textSize="15sp" /> <Button android:id="@+id/alertdialog_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="单选对话框" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alertdialog_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="多选对话框" android:textSize="15sp" /> <Button android:id="@+id/alertdialog_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="列表对话框" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alertdialog_div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="自定义对话框" android:textSize="15sp" /> <Button android:id="@+id/notification_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="发送消息到通知栏" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/notification_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shapemore" android:text="取消消息到通知栏" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
接下来获取布局中控件的id:
// 发送消息到通知栏,取消通知栏的消息 Button notification_send, notification_cancel; NotificationManager notificationManager; //标识 int notification_id;
// 获取控件id private void initView() { // Notification notification_send = (Button) findViewById(R.id.notification_send); notification_cancel = (Button) findViewById(R.id.notification_cancel); }
建立点击事件
// 创建点击事件 private void initEvent() { // Notification点击事件 notification_send.setOnClickListener(this); notification_cancel.setOnClickListener(this); }
发送效果图:
/** * 发送通知到通知栏 */ @SuppressLint("NewApi") private void sendNotification() { android.app.Notification.Builder builder = new Notification.Builder( this); builder.setSmallIcon(R.drawable.ic_launcher);// 设置图标 builder.setTicker("你好");// 设置手机状态栏的提示 builder.setWhen(System.currentTimeMillis());// 设置时间 builder.setContentTitle("通知栏通知");// 设置标题 builder.setContentText("我来自demo");// 设置内容 Toast.makeText(MainActivity.this, "发送消息到通知栏", Toast.LENGTH_LONG).show(); Intent intent = new Intent(this, MainActivity.class); PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0); builder.setContentIntent(pending);// 点击后的意图 builder.setDefaults(Notification.DEFAULT_LIGHTS);// 设置指示灯 builder.setDefaults(Notification.DEFAULT_SOUND);// 设置提示声音 builder.setDefaults(Notification.DEFAULT_VIBRATE);// 设置震动 builder.setDefaults(Notification.DEFAULT_ALL);// 包含以上三个 // 通过Builder对象的build()可以创建一个notification对象 Notification notification = builder.build();// 4.1以上(包括4.1),4.1以下用builder.getNotification(); // 创建NotificationManager的对象 notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 显示notification notificationManager.notify(notification_id, notification); }
取消效果图:
取消发送的代码就相对来说简洁很多
/** * 取消到通知栏的消息 */ private void sendCancelNotification() { notificationManager.cancel(notification_id); Toast.makeText(MainActivity.this, "取消通知栏的消息", Toast.LENGTH_LONG).show(); }
最后还需要说一下,如果想让你的消息发送到通知栏有震动等提示的话,还需要去AndroidManifest.xml中设置指示灯和震动的权限
users Permission
android.permission.FLASHLIGHT
android.permission.VIBRATE
相关文章推荐
- Android工程中三种库的区别:Private Libraries、Referenced Libraries、Dependency Libraries
- Android 拍照 图库的各种问题.
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- Android开发调试必备 - 使用DDMS
- Android的Activity屏幕切换动画(一)-左右滑动切换overridePendingTransition
- android TextView的xml属性一览表
- Android开发_如何设置按钮背景透明与半透明_图片背景透明
- Android native debug: 手动使用gdbserver远程调试C代码
- 【转】渗透测试环境搭建常用命令方法checklist(iOS&&Android)
- 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
- Android中图片优化之webp使用
- Android中图片优化之webp使用
- Android中图片优化之webp使用
- Android后台进程与前台线程间的区别使用
- Android后台进程与前台线程间的区别使用
- 可能被你忽略的TextView属性
- Android-开发自定义组件
- Android开发之json解析
- Android与web服务器数据交互编程---2-如何实现手机与web的通信---server实现
- Android TextView 嵌套图片及其点击,TextView 部分文字点击,文字多颜色