Android使用Notification实现普通通知栏(一)

所属分类: 软件编程 / Android 阅读数: 21
收藏 0 赞 0 分享

Notification是在你的应用常规界面之外展示的消息。当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏。要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看。(notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app。

Notification的设计:

作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则。

Notification的界面元素在通知抽屉中的notification有两种显示方式,取决于你的android版本以及notificationdrawer的状态。

Notification的两种显示方式:

(1)普通视图

这种风格是notification drawer的标准显示方式。

(2)宽视图

指你的notification被展开的时候会显示更大的视图,这种风格是android4.1之后才有的新特性。

下面我们详细介绍普通视图的实现:

在图通视图中,notification最高64dp,即使你创建了一个宽视图风格的notification,在未展开的情况下也是以普通大小显示出来。下面是一个普通的notification。

蓝色指示框所代表的的意思如下:

1.标题

2.大图标

3.通知内容

4.通知数据

5.小图标

6.Notification的发布时间。

可以通过调用setWhen()设置一个明确的时间,

默认是系统收到该notification的时间。

下面我们是我们本次的演示效果:


本次在普通视图的基础上添加了点击页面跳转的效果,可以理解为添加Notification的动作与行为:

虽然这也是可选的,但是你还是应该为你的notification至少添加一种行为:允许用户通过点击notification进入一个activity中进行更多的查看或者后续操作。一个notification可以提供多种动作,而且你也应该让用户点击一个notification之后能总是有相应的响应动作,通常是打开一个activity。你还可以在notification中添加能响应点击事件的button,比如延迟一下闹钟,或者立即回复一条短消息。

在notification内部,一个动作本身是被定义在一个PendingIntent中,PendingIntent包含了一个用于启动你app中activity的intent。要将PendingIntent和一个手势联系起来,你需要调用合适的NotificationCompat.Builder方法。

比如你想在点击notification文字的时候启动activity,你需要调用NotificationCompat.Builder的setContentIntent()来添加PendingIntent。启动一个activity是notification动作响应中最普遍的一类。

第一步:Layout中的activity_main.xml(仅设置触发按钮):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.administrator.day12.MainActivity">
 <Button
  android:text="显示通知"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/button"
  android:onClick="show1" />
</LinearLayout>

第二步:Layout中的跳转页面activity_content.xml(仅设置显示文本):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_content"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.administrator.day12.ContentActivity">
 <TextView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:textSize="30sp"
  android:text="十胜十败" />
</LinearLayout>

第三步:java(主界面按钮的点击事件)实现代码MainActivity.java:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
 private static final int NO_1 =0x1 ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 public void show1(View v){
  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  builder.setSmallIcon(R.mipmap.guojia);
  builder.setContentTitle("郭嘉");
  builder.setContentText("我们打袁绍吧");
  //设置Notification.Default_ALL(默认启用全部服务(呼吸灯,铃声等)
  builder.setDefaults(Notification.DEFAULT_ALL);
  //调用NotificationCompat.Builder的setContentIntent()来添加PendingIntent
  Intent intent = new Intent(this, ContentActivity.class);
  intent.putExtra("info", "郭嘉给你发了一个计策!");
  PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  builder.setContentIntent(pi);
  //获取Notification
  Notification n = builder.build();
  //通过NotificationCompat.Builder.build()来获得notification对象自己
  NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  //然后调用NotificationManager.notify()向系统转交
  manager.notify(NO_1, n);
 }
} 

第四步:java(跳转后Activity)功能代码实现ContentActivity.java(只土司):

public class ContentActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_content);
  //通过获取MainActivity中设置的putExtra获取土司内容
  Toast.makeText(this, getIntent().getStringExtra("info"), Toast.LENGTH_SHORT).show();
 }
}

演示效果的代码就这些,我们梳理下本次实现的思路:

(1)通过按钮触发点击事件

(2)将notification的一些UI信息以及相关动作赋予NotificationCompat.Builder对象,然后通过NotificationCompat.Builder.build()来获得notification对象自己;然后调用NotificationManager.notify()向系统转交这个通知。

(3)在第二步中通过Builder的setContentIntent()来添加PendingIntent,为Notification添加行为,也就是Activity的跳转

(4)对打开的Activity设置表现的效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多