Android入门(43)——第五章 使用Notification实现状态通知栏的通知
来源:互联网 发布:90淘宝美工素材 编辑:程序博客网 时间:2024/06/05 17:22
1. 简介:
2. 如何实现:
通知管理类:
3. 案例一:
第一步:创建布局文件:activity_main.xml:包括两个按钮,一个是发送通知,一个是取消通知。
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/btn_send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送通知" > </Button> <Button android:id="@+id/btn_cancle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消通知" > </Button> </LinearLayout></RelativeLayout>第二步:在Manifest中的Permissions中添加两个用户权限,分别是:第一个用来授权指示灯,第二个授权震动。
第三步:修改MainActivity文件:
package com.example.notification;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog.Builder;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.view.View;public class MainActivity extends Activity implements android.view.View.OnClickListener{// 通知控制类NotificationManager manager;// 设置一个id,但是并没有一个值。int notification_ID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 设置点击事件 findViewById(R.id.btn_send).setOnClickListener(this); findViewById(R.id.btn_cancle).setOnClickListener(this); // 获取的是一个系统服务 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); }@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch(arg0.getId()){case R.id.btn_send:sendNotification();break;case R.id.btn_cancle:// 取消已发送的某个通知:manager.cancel(notification_ID);break;default:break;}} /* * 构造notification,并发送到通知栏。 * */private void sendNotification(){// 下面这两行是为了下面的setContentIntent而添加的:就是点击通知栏之后的响应跳转。Intent intent = new Intent(this,MainActivity.class);// 第二个参数requestCode请求码,那这个点击后的意图呢就是启动MainActivity这个类。PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);// 第一步:创建这个builder就是为了设置各种显示和意图:android.app.Notification.Builder builder = new Notification.Builder(this);// 设置图标提示:builder.setSmallIcon(R.drawable.ic_launcher);// 设置手机状态栏的提示:builder.setTicker("hello");// 设置时间:builder.setWhen(System.currentTimeMillis());// 设置标题:builder.setContentTitle("通知栏通知");// 设置通知内容:builder.setContentText("我来自Notification案例");// 设置点击后的意图:builder.setContentIntent(pintent);// 设置声音提示:builder.setDefaults(Notification.DEFAULT_SOUND);// 设置提示指示灯,需要权限builder.setDefaults(Notification.DEFAULT_LIGHTS);// 设置震动效果,需要权限builder.setDefaults(Notification.DEFAULT_VIBRATE);// 设置上面的三条://builder.setDefaults(Notification.DEFAULT_ALL);// 第二步:然后通过 builder.build();来获取notification。// 4.1以上要用builder.build();,以下用builder.getNotification();Notification notification = builder.build();//Notification notification = builder.getNotification();// 第三步:通过NotificationManager来显示notification。manager.notify(notification_ID,notification);}}效果图:
0 0
- Android入门(43)——第五章 使用Notification实现状态通知栏的通知
- 【Android】使用Notification实现状态通知栏的通知
- Android——Notification状态通知栏
- 使用Notification实现状态通知栏的发送和取消
- android通知的使用——Notification
- Notification实现状态通知栏
- android的通知栏——Notification
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
- Android通知栏Notification的实现
- 通知栏通知:Notification的实现
- Notification消息通知 通知栏的实现
- android中通知栏notification的使用
- android通知栏(Notification)的使用
- android通知栏之Notification的使用
- Android--Notification通知栏的使用
- Android的通知(Notification)使用详解
- android 通知的使用(Notification)
- Android的通知(Notification)使用详解
- IOS上AudioSession设置, 切换扬声器和听筒详解 .
- Unity3d之MonoBehaviour的可重写函数整理
- 20-_el_expressionfactory=_jspxFactory.getJspApplicationContext(getServletConfig()
- 【链表】判断单链表是否是回文链表
- 调整容器中控件索引
- Android入门(43)——第五章 使用Notification实现状态通知栏的通知
- iOS - 动画Animation零基础完全教程之一
- ANDROID技术积累:开发规范
- Linux安装VMware Tools 录屏
- InnoDB Insert(插入)操作(下)--mysql技术内幕
- Crazyflie 2.0 System Architecture
- c#设计模式---建造者模式(Builder Pattern)
- c++primer 要点-函数
- 【读书笔记】Linux高性能服务器编程(第二篇 第五章)