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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

潘侯爷   2021-03-22 我要评论
想了解Android使用Notification实现普通通知栏(一)的相关内容吗,潘侯爷在本文为您仔细讲解Notification通知栏的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,Notification,通知栏,下面大家一起来学习吧。

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设置表现的效果。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们