• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Android中使用Notification实现普通通知栏(Notification示例一),rest示例java实现

Android中使用Notification实现普通通知栏(Notification示例一),rest示例java实现

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了Android中使用Notification实现普通通知栏(Notification示例一),rest示例java实现等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Android中使用Notification实现普通通知栏(Notification示例一),rest示例java实现


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(仅设置触发按钮):

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:id="@+id/activity_main"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     tools:context="com.example.administrator.day12.MainActivity">
 9     <Button
10         android:text="显示通知"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:id="@+id/button"
14         android:onClick="show1" />
15 </LinearLayout>

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_content"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.administrator.day12.ContentActivity">
 8     <TextView
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:gravity="center"
12         android:textSize="30sp"
13         android:text="十胜十败" />
14 </LinearLayout>

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

 1 import android.app.Notification;
 2 import android.app.NotificationManager;
 3 import android.app.PendingIntent;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.graphics.BitmapFactory;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.support.v7.app.NotificationCompat;
10 import android.view.View;
11 import android.widget.RemoteViews;
12 public class MainActivity extends AppCompatActivity {
13     private static final int NO_1 =0x1 ;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18     }
19     public  void show1(View v){
20         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
21         builder.setSmallIcon(R.mipmap.guojia);
22         builder.setContentTitle("郭嘉");
23         builder.setContentText("我们打袁绍吧");
24         //设置Notification.Default_ALL(默认启用全部服务(呼吸灯,铃声等)
25         builder.setDefaults(Notification.DEFAULT_ALL);
26         //调用NotificationCompat.Builder的setContentIntent()来添加PendingIntent
27         Intent intent = new Intent(this, ContentActivity.class);
28         intent.putExtra("info", "郭嘉给你发了一个计策!");
29         PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
30         builder.setContentIntent(pi);
31         //获取Notification
32         Notification n = builder.build();
33         //通过NotificationCompat.Builder.build()来获得notification对象自己
34         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
35         //然后调用NotificationManager.notify()向系统转交
36         manager.notify(NO_1, n);
37     }
38 }

 

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

1 public class C



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android中使用Notification实现普通通知栏(Notification示例一),rest示例java实现

相关文章

  • 2017-05-26Android 查看自己的keystore的别名及相关信息,androidkeystore
  • 2017-05-26Android中的沉浸式状态栏效果,android沉浸状态栏
  • 2017-05-26Android开发艺术探索学习笔记(十一),android艺术探索
  • 2017-05-26Android的Kotlin秘方(I):OnGlobalLayoutListener,
  • 2017-05-26Android中使用SDcard进行文件的读取,androidsdcard
  • 2017-05-26Android滑动删除功能,android滑动删除
  • 2017-05-26Andriod DiskLruCache的使用案例
  • 2017-05-26Google官方MVP模式示例项目解析 todo-mvp,mvptodo-mvp
  • 2017-05-26andriod绘制图形,andriod绘制
  • 2017-05-26Android 权限的实现,Android权限实现

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android学习笔记-Button(按钮),android-button
    • Android中如何修改编译的资源ID值(默认值是0x7F...可以随意改成0x02~0x7E)
    • Android百度地图API集成一《基础地图》,集成百度地图api
    • Android中使用开源框架citypickerview实现省市区三级联动选择,
    • 3.4 TouchListener PK OnTouchEvent + 多点触碰
    • 在Android Studio 配置OpenCV 3.1
    • 搜索保存历史记录功能,保存历史记录功能
    • RK3288开发过程中遇到的问题点和解决方法之Kernel,rk3288kernel
    • 关于百度地图导航AndroidSDK的初始化问题,androidsdk初始化
    • 7.5.6 WebView处理网页返回的错误码信息

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有