分类: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. (转)Sql Server 快速查看表结构(表描述及字段说明)

    --表描述 SELECT tbs.name 表名,ds.value 描述 FROM sys.extended_properties ds LEFT JOIN sysobjects tbs ON ds. ...

  2. BP反向传播算法的工作原理How the backpropagation algorithm works

    In the last chapter we saw how neural networks can learn their weights and biases using the gradient ...

  3. Python学习(四)数据结构 —— set frozenset

    集合类型 set  frozenset 赋值及去重 set 是一个无序不重复元素集,还有个frozenset 类型(顾明思议,就是不可改变元素的集合): 基本功能包括关系测试和消除重复元素:set支持 ...

  4. Resin install document

    Centos6快速安装文档 resin3.1.13 软件下载地址: http://caucho.com/products/resin/download/gpl#download #系统环境[root@ ...

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

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

  6. 正向代理/反向代理理解、Nginx概述、安装及配置详解

    一.Nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理, ...

  7. jsp+servlet+jdbc实现对数据库的增删改查

    一.JSP和Servlet的简单介绍: 1.Servlet和JSP简介: Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP,Servlet是在服务器端执行的Java程序,一个 ...

  8. iOS 本地通知 操作

    iOS 本地通知 操作 1:配置通知:然后退出程序: UILocalNotification *localNotif = [[UILocalNotification alloc] init]; loc ...

  9. openerp在ubuntu中查看日志

    sudo su - openerp -s /bin/bash less /var/log/openerp/openerp-server.log

  10. windows合并文件夹窗口

      windows合并文件夹窗口 CreateTime--2017年7月26日16:28:14Author:Marydon 右击任务栏-->属性-->任务栏按钮选项-->选择“始终合 ...