分类:C#、Android、VS2015;

创建日期:2016-02-29

一、简介

上一节介绍了通知(Notification)相关的基本内容。这一节先用一个简单示例演示创建和发布本地通知的基本用法,下一节再演示更多的用法。

二、示例2运行截图

在穿ch1502MainActivity屏幕上(左侧图)单击【发布通知】按钮后,屏幕左上角就会显示一个通知图标,下拉该图标,就会显示通知区域(中间的图)。单击通知区域中上面的那个示例通知,就会显示ch1502SecondActivity屏幕(未截图),并在此屏幕上显示在ch1502MainActivity屏幕中单击按钮的次数。

 

三、主要设计步骤

1、添加通知用的图标

在drawable文件夹下添加一个ch1502statButton.png图片,作为通知的图标。或者自己做一个图标。

2、添加ch1502_Main.axml

在layout文件夹下添加该文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/ch1502_btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发布通知" />
</LinearLayout>

3、添加ch1502_Second.axml文件

在layout文件夹下添加该文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ch1502_textView1" />
</LinearLayout>

4、添加ch1502SecondActivity.cs文件

在SrcDemos文件夹下添加该文件。

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "【例15-2】Notification基本用法")]
public class ch1502SecondActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // 获取MainActivity中传递的count值,如果不存在count则返回-1
int count = Intent.Extras.GetInt("count", -1); // 如果没有传递count,直接返回
if (count <= 0)
{
return;
} SetContentView(Resource.Layout.ch1502_Second);
TextView txtView = FindViewById<TextView>(Resource.Id.textView1);
txtView.Text = string.Format("你单击了 {0} 次按钮。", count);
}
}
}

5、添加ch1502MainActivity.cs文件

在SrcDemos文件夹下添加该文件。

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "ch1502MainActivity")]
public class ch1502MainActivity : Activity
{
// 通知的唯一ID号
private static readonly int ButtonClickNotificationId = 1000; // 单击按钮的次数
private int count = 1; protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1502_Main);
Button button = FindViewById<Button>(Resource.Id.ch1502_btn1);
button.Click += Button_Click;
} private void Button_Click(object sender, EventArgs e)
{
// 传递count到下一个activity:
Bundle valuesForActivity = new Bundle();
valuesForActivity.PutInt("count", count); // 当用户单击通知时,启动SecondActivity
Intent resultIntent = new Intent(this, typeof(ch1502SecondActivity)); resultIntent.PutExtras(valuesForActivity); // 为跨任务导航构造back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(ch1502SecondActivity)));
stackBuilder.AddNextIntent(resultIntent); // 为 back stack 创建 PendingIntent
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent); // 创建通知
Notification.Builder builder = new Notification.Builder(this)
.SetAutoCancel(true) // 单击通知时取消通知(让通知消失)
.SetContentIntent(resultPendingIntent) // 单击Intent时启动activity
.SetContentTitle("简单通知示例") // 标题
.SetNumber(count) // 在Content Info中显示count的值
.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate) //显示通知时播放声音并振动
.SetSmallIcon(Resource.Drawable.ch1502statButton) // 显示的通知图标
.SetContentText(string.Format("你已经单击了 {0} 次按钮。", count)); // 显示的消息 // 发布通知
NotificationManager notificationManager = GetSystemService(NotificationService) as NotificationManager;
notificationManager.Notify(ButtonClickNotificationId, builder.Build()); // 按钮次数加1
count++;
}
}
}

【Android】15.4 例15-2--Notification基本用法的更多相关文章

  1. 【Android】15.5 例15-3—Notification的各种属性演示

    分类:C#.Android.VS2015: 创建日期:2016-02-29 一.简介 利用这个例子,可测试通知的各种属性以及这些不同属性选项呈现的效果. 另外,在这个例子中,还演示了如何读写SD中的图 ...

  2. 【Android】15.0 第15章 广播和通知—本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-28 一.简介 广播(Broadcast):其功能类似于收音机的广播,你只要调到那个台(只要在接收的类中注册了要接收的广播),就能 ...

  3. [转]phoneGap3.0安装步骤(以windows下的android环境为例):

    phoneGap3.0安装步骤(以windows下的android环境为例): 环境: WIN系统,JDK,Android,Eclipse,Ant,Git,PhoneGap3.x (Cordova) ...

  4. Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法

    Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法 Summary的用法和Group一样简单,分为两步: 启用Summary功能 在Feature标签内,添加如 ...

  5. Data truncation: Incorrect datetime value: 'May 15, 2019 4:15:37 PM

    因为系统在windows下测试过是正常的 windows下的jdk+ windows下安装的mysql 全部cases通过 linux下的jdk + windows下安装的mysql 新增和更新,影响 ...

  6. JAVA 基础编程练习题15 【程序 15 排序】

    15 [程序 15 排序] 题目:输入三个整数 x,y,z,请把这三个数由小到大输出. 程序分析:我们想办法把最小的数放到 x 上,先将 x 与 y 进行比较,如果 x>y 则将 x 与 y 的 ...

  7. Python:安装opencv出现错误Could not find a version that satisfies the requirement numpy==1.13.3 (from versions: 1.14.5, 1.14.6, 1.15.0rc2, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0rc1, 1.16.0rc2,

    安装opencv的时候,出现numpy的版本不匹配,卸载了不匹配的版本,重新安装却是一点用都没有,后面尝试了一下这里的提示pip更新,居然安装成功了,看来pip的版本过低真是误事啊. 报错是: Cou ...

  8. iOS7编程Cookbook中例15.8中一个小问题

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 该书的15.8例子标题为Editing Videos on a ...

  9. Android菜鸟成长记15 -- BitMap

    BitMap简介 Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件.本文从应用的角度,着重介绍怎么用Bi ...

随机推荐

  1. List of Chromium Command Line Switches(命令行开关集)——官方指定命令行更新网址

    转自:http://peter.sh/experiments/chromium-command-line-switches/ There are lots of command lines which ...

  2. 用IIS防止mdb数据库被下载

    如何防止mdb数据库被下载?本文讨论的是在服务器端禁止mdb格式数据库文件被下载,而不是在数据库中加入防下载表,将数据库名改为含#号的asp.asa等后缀格式. 下面以IIS6.0为例说明如何在服务器 ...

  3. go语言基础之全局变量

    1.全局变量 示例: package main import "fmt" func test() { fmt.Println("test a =", a) } ...

  4. SQL Server 2012不支持Microsoft Visual Studio Test Controller 2010

    折腾了一个上午, 发现Test Controller怎么都连不上SQL. 能尝试的都尝试了, 觉得应该看看是不是有不支持的问题.   找到了这篇. TFS 2010 will not support ...

  5. (转)D3d9c的固定渲染管道(fixed function pipeline)与可编程管道(programmable function pipeline)的异同点

    转自:http://blog.csdn.net/tspatial_thunder/article/details/5937701 现在的游戏图形部分越来多依靠GPU来渲染绘制.说起GPU先说着色器,着 ...

  6. 统计的一个小题目python实现

    最近面试碰到的一个题目,业余时间用python实现的. 拿到数据,先用sort 命令排序,也可再进一步去重复 sort -k 1,2  data.txt |uniq  > data.new # ...

  7. Android之横屏竖屏显示问题

    1.採用不同的布局文件 在res下创建目录layout-land 在该目录下写入的layout(xml文件)横屏的时候系统自己主动选择显示的layout 同理: 在res下创建目录layout-por ...

  8. Pureftp SSL/TLS配置

    一.准备 & 安装 启用 Pure-FTPd SSL/TLS 连接方式在安装时需要检查以下两项: 1.系统中是否已经安装了 openssl 和 openssl-devel 包? 2.在编译 P ...

  9. js冒泡法和数组转换成字符串

    js代码: window.onload = function(){ var mian = document.getElementById( "mian" ); var mian1 ...

  10. MPMoviePlayerViewController 视频播放黑屏

    MPMoviePlayerViewController 视频播放黑屏 今天用视频做本地视频播放:使用 MPMoviePlayerViewController 老是出现黑屏: 结果发现是一个 很坑爹的问 ...