分类:C#、Android、VS2015;

创建日期:2016-02-29

一、简介

利用这个例子,可测试通知的各种属性以及这些不同属性选项呈现的效果。

另外,在这个例子中,还演示了如何读写SD中的图像文件。

二、示例3运行截图

 

三、主要设计步骤

本示例使用了下面的图像文件:

ch1503_monkey_icon.png

ch1503_notification.png

ch1503_x_bldg.jpg

如果你不想使用这些文件,也可以用其他的图像文件代替。

1、添加ch1503arrays.xml

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="notification_style">
<item>Normal</item>
<item>Big Text</item>
<item>Inbox</item>
<item>Image</item>
</string-array>
<string-array name="notification_visibility">
<item>Public</item>
<item>Private</item>
<item>Secret</item>
</string-array>
<string-array name="notification_priority">
<item>Default</item>
<item>High</item>
<item>Low</item>
<item>Maximum</item>
<item>Minimum</item>
</string-array>
<string-array name="notification_category">
<item>Call</item>
<item>Message</item>
<item>Alarm</item>
<item>Email</item>
<item>Event</item>
<item>Promo</item>
<item>Progress</item>
<item>Social</item>
<item>Error</item>
<item>Transport</item>
<item>System</item>
<item>Service</item>
<item>Recommendation</item>
<item>Status</item>
</string-array>
</resources>

2、添加权限(如果已经有这些权限就不用添加了)

在AndroidManifest.xml文件中添加下面的权限(允许震动、允许读取外部存储):

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3、添加ch1503_Main.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="这是通知的标题" />
<EditText
android:id="@+id/notifyText"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_below="@+id/textView1"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="这是通知的详细信息。" />
<TextView
android:id="@+id/textViewStyle"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:layout_marginBottom="2dp"
android:layout_below="@+id/notifyText"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Style:" />
<Spinner
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2dp"
android:layout_below="@+id/notifyText"
android:layout_toRightOf="@+id/textViewStyle"
android:layout_alignBaseline="@+id/textViewStyle"
android:id="@+id/styleSpinner" />
<TextView
android:id="@+id/textViewVisibility"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:layout_marginBottom="2dp"
android:layout_below="@+id/styleSpinner"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Visibility:" />
<Spinner
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2dp"
android:layout_below="@+id/styleSpinner"
android:layout_toRightOf="@+id/textViewVisibility"
android:layout_alignBaseline="@+id/textViewVisibility"
android:id="@+id/visibilitySpinner" />
<TextView
android:id="@+id/textViewPriority"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:layout_marginBottom="2dp"
android:layout_below="@+id/visibilitySpinner"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Priority:" />
<Spinner
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="2dp"
android:layout_below="@+id/visibilitySpinner"
android:layout_toRightOf="@+id/textViewPriority"
android:layout_alignBaseline="@+id/textViewPriority"
android:id="@+id/prioritySpinner" />
<TextView
android:id="@+id/textViewCategory"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/prioritySpinner"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Category:" />
<Spinner
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_below="@+id/prioritySpinner"
android:layout_toRightOf="@+id/textViewCategory"
android:layout_alignBaseline="@+id/textViewCategory"
android:id="@+id/categorySpinner" />
<Switch
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:layout_marginBottom="10dp"
android:layout_below="@+id/categorySpinner"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Large Icon:"
android:id="@+id/largeIconSwitch" />
<Switch
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:layout_marginBottom="10dp"
android:layout_below="@+id/largeIconSwitch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sound:"
android:id="@+id/soundSwitch" />
<Switch
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:layout_below="@+id/soundSwitch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Vibrate:"
android:id="@+id/vibrateSwitch" />
<Button
android:text="发送通知"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_below="@+id/vibrateSwitch"
android:id="@+id/launchButton" />
</RelativeLayout>

4、添加ch1503_Second.axml

<?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:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView1" />
</LinearLayout>

5、添加ch1503SecondActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget; namespace MyDemos.SrcDemos
{
[Activity(Label = "ch1503SecondActivity")]
public class ch1503SecondActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
string message = Intent.Extras.GetString("message", "");
SetContentView(Resource.Layout.ch1503_Second);
TextView textView = FindViewById<TextView>(Resource.Id.textView1);
textView.Text = string.Format("传递到SecondActivity的消息是:\n {0}", message);
}
}
}

6、添加ch1503MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Android.Graphics; namespace MyDemos.SrcDemos
{
[Activity(Label = "【例15-3】通知的各种基本用法")]
public class ch1503MainActivity : Activity
{
EditText notifyMsg;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1503_Main); var notificationManager = GetSystemService(NotificationService) as NotificationManager; // 可在文本框中直接编辑通知消息:
notifyMsg = FindViewById<EditText>(Resource.Id.notifyText); #region 设置在Resources/values/arrays.xml中定义的下拉框(Spinners)
//样式下拉框
Spinner styleSpinner = FindViewById<Spinner>(Resource.Id.styleSpinner);
var styleAdapter = ArrayAdapter.CreateFromResource(this,
Resource.Array.notification_style,
Android.Resource.Layout.SimpleSpinnerDropDownItem);
styleSpinner.Adapter = styleAdapter;
// 选择下拉框时控制文本框的样式:
styleSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(styleSpinnerSelected); //可见性下拉框
Spinner visibilitySpinner = FindViewById<Spinner>(Resource.Id.visibilitySpinner);
var visibilityAdapter = ArrayAdapter.CreateFromResource(this,
Resource.Array.notification_visibility,
Android.Resource.Layout.SimpleSpinnerDropDownItem);
visibilitySpinner.Adapter = visibilityAdapter; //优先级下拉框
Spinner prioritySpinner = FindViewById<Spinner>(Resource.Id.prioritySpinner);
var priorityAdapter = ArrayAdapter.CreateFromResource(this,
Resource.Array.notification_priority,
Android.Resource.Layout.SimpleSpinnerDropDownItem);
prioritySpinner.Adapter = priorityAdapter; // 分类下拉框
Spinner categorySpinner = FindViewById<Spinner>(Resource.Id.categorySpinner);
var categoryAdapter = ArrayAdapter.CreateFromResource(this,
Resource.Array.notification_category,
Android.Resource.Layout.SimpleSpinnerDropDownItem);
categorySpinner.Adapter = categoryAdapter;
#endregion #region 选项开关
// 获取布局中的大图标、声音、振动开关:
Switch largeIconSw = FindViewById<Switch>(Resource.Id.largeIconSwitch);
Switch soundSw = FindViewById<Switch>(Resource.Id.soundSwitch);
Switch vibrateSw = FindViewById<Switch>(Resource.Id.vibrateSwitch);
#endregion #region 执行通知的按钮
Button launchBtn = FindViewById<Button>(Resource.Id.launchButton);
launchBtn.Click += delegate
{
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("Sample Notification")
.SetContentText(notifyMsg.Text)
.SetSmallIcon(Resource.Drawable.ch1503_notification)
.SetAutoCancel(true); if (largeIconSw.Checked)
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.ch1503_monkey_icon)); switch (styleSpinner.SelectedItem.ToString())
{
case "Big Text":
builder.SetContentTitle("Big Text Notification");
var textStyle = new Notification.BigTextStyle();
textStyle.BigText(notifyMsg.Text);
textStyle.SetSummaryText("The summary text goes here.");
builder.SetStyle(textStyle);
break;
case "Inbox":
var inboxStyle = new Notification.InboxStyle();
builder.SetContentTitle("5 条新消息");
builder.SetContentText("abc@126.com");
inboxStyle.AddLine("张三:香蕉便宜了");
inboxStyle.AddLine("李四:那事咋样了");
inboxStyle.AddLine("王五:别忘了给我回邮件");
inboxStyle.SetSummaryText("+2 more");
builder.SetStyle(inboxStyle);
break;
case "Image":
builder.SetContentTitle("Image Notification");
var picStyle = new Notification.BigPictureStyle(); //用法1
picStyle.BigPicture(BitmapFactory.DecodeResource(Resources, Resource.Drawable.ch1503_x_bldg));
picStyle.SetSummaryText("The summary text goes here."); // 用法2(从SD卡中读取图像)
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.InSampleSize = 2;
// string imagePath = "/sdcard/Pictures/my-tshirt.jpg";
// picStyle.BigPicture(BitmapFactory.DecodeFile(imagePath, options));
// picStyle.SetSummaryText("Check out my new T-shirt!"); builder.SetStyle(picStyle);
break;
default:
// 使用默认的通知样式
break;
} switch (visibilitySpinner.SelectedItem.ToString())
{
case "Public": builder.SetVisibility(NotificationVisibility.Public); break;
case "Private": builder.SetVisibility(NotificationVisibility.Private); break;
case "Secret": builder.SetVisibility(NotificationVisibility.Secret); break;
} switch (prioritySpinner.SelectedItem.ToString())
{
case "High": builder.SetPriority((int)NotificationPriority.High); break;
case "Low": builder.SetPriority((int)NotificationPriority.Low); break;
case "Maximum": builder.SetPriority((int)NotificationPriority.Max); break;
case "Minimum": builder.SetPriority((int)NotificationPriority.Min); break;
default: builder.SetPriority((int)NotificationPriority.Default); break;
} switch (categorySpinner.SelectedItem.ToString())
{
case "Call": builder.SetCategory(Notification.CategoryCall); break;
case "Message": builder.SetCategory(Notification.CategoryMessage); break;
case "Alarm": builder.SetCategory(Notification.CategoryAlarm); break;
case "Email": builder.SetCategory(Notification.CategoryEmail); break;
case "Event": builder.SetCategory(Notification.CategoryEvent); break;
case "Promo": builder.SetCategory(Notification.CategoryPromo); break;
case "Progress": builder.SetCategory(Notification.CategoryProgress); break;
case "Social": builder.SetCategory(Notification.CategorySocial); break;
case "Error": builder.SetCategory(Notification.CategoryError); break;
case "Transport": builder.SetCategory(Notification.CategoryTransport); break;
case "System": builder.SetCategory(Notification.CategorySystem); break;
case "Service": builder.SetCategory(Notification.CategoryService); break;
case "Recommendation": builder.SetCategory(Notification.CategoryRecommendation); break;
case "Status": builder.SetCategory(Notification.CategoryStatus); break;
default: builder.SetCategory(Notification.CategoryStatus); break;
}
#endregion Intent secondIntent = new Intent(this, typeof(ch1503SecondActivity));
secondIntent.PutExtra("message", notifyMsg.Text);
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(ch1503SecondActivity)));
stackBuilder.AddNextIntent(secondIntent);
const int pendingIntentId = 0;
PendingIntent pendingIntent = stackBuilder.GetPendingIntent(pendingIntentId, PendingIntentFlags.OneShot); // 下面的代码用于从通知返回到MainActivity
// Intent intent = new Intent (this, typeof(MainActivity));
// const int pendingIntentId = 0;
// pendingIntent = PendingIntent.GetActivity (this, pendingIntentId, intent, PendingIntentFlags.OneShot);
// builder.SetContentText("Hello World! This is my first action notification!"); builder.SetContentIntent(pendingIntent); Notification notification = builder.Build();
if (soundSw.Checked) notification.Defaults |= NotificationDefaults.Sound;
if (vibrateSw.Checked) notification.Defaults |= NotificationDefaults.Vibrate;
const int notificationId = 1;
notificationManager.Notify(notificationId, notification); // 下面的代码用于5秒后更新通知
// Thread.Sleep(5000);
// builder.SetContentTitle("Updated Notification");
// builder.SetContentText("Changed to this message after five seconds.");
// notification = builder.Build();
// notificationManager.Notify(notificationId, notification);
};
} private void styleSpinnerSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = sender as Spinner;
switch (spinner.SelectedItem.ToString())
{
case "Big Text":
notifyMsg.Text = "这是通知消息。你可以在文本框中修改要发送的消息,";
notifyMsg.Text += "然后单击发送消息按钮观察效果。";
break;
case "Big Picture":
notifyMsg.Text = "该通知包含一个图像。发送通知后,可下拉通知观察图像。";
break;
case "Inbox":
notifyMsg.Text = "该通知包含邮件消息的摘要。发送通知后,可下拉通知观察这些摘要。";
break;
default:
notifyMsg.Text = "Notification message text goes here.";
break;
}
}
}
}

【Android】15.5 例15-3—Notification的各种属性演示的更多相关文章

  1. 【Android】15.4 例15-2--Notification基本用法

    分类:C#.Android.VS2015: 创建日期:2016-02-29 一.简介 上一节介绍了通知(Notification)相关的基本内容.这一节先用一个简单示例演示创建和发布本地通知的基本用法 ...

  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. Data truncation: Incorrect datetime value: 'May 15, 2019 4:15:37 PM

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

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

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

  6. share point 已在此服务器场中安装 ID 为 15/b7a69889-1789-4855-b8bd-9a3b4cfd7fc0 的功能。请使用强制属性显式地重新安装此功能。

    部署步骤“添加解决方案”中出现错误: 已在此服务器场中安装 ID 为 15/b7a69889-1789-4855-b8bd-9a3b4cfd7fc0 的功能.请使用强制属性显式地重新安装此功能. 英文 ...

  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. Android零基础入门第80节:Intent 属性详解(下)

    上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...

  9. Android webview 写入cookie的解决方法以及一些属性设置

    原文地址:https://www.2cto.com/kf/201703/616868.html Android webview 写入cookie的解决方法以及一些属性设置,webview怎么设置写入C ...

随机推荐

  1. soa文章摘抄

    from: http://blog.vsharing.com/fengjicheng/MC19136/ 浅析深究什么是SOA? (入选推荐日志,加10币)浅析深究什么是SOA? 金蝶中间件有限公司总经 ...

  2. OpenCV特征点检测

    特征点检测 目标 在本教程中,我们将涉及: 使用 FeatureDetector 接口来发现感兴趣点.特别地: 使用 SurfFeatureDetector 以及它的函数 detect 来实现检测过程 ...

  3. OpenCV定制化创建角点检测子

    定制化创建角点检测子 目标 在这个教程中我们将涉及: 使用 OpenCV 函数 cornerEigenValsAndVecs 来计算像素对应的本征值和本征向量来确定其是否是角点. 使用OpenCV 函 ...

  4. iOS:UI简单的总结

    UI简单总结: 一.常用单例: NSBundle *bundel = [NSBundle mainBundle]; //加载资源 NSFileManager *fm = [NSFileManager  ...

  5. iOS:文件归档和解归档的详解和使用

    文件归档和解归档: 用途: 所谓文件归档,就是把需要存储的对象数据存储到沙盒的Documents目录下的文件中,即存储到了磁盘上,实现数据的持久性存储和备份.解归档,就是从磁盘上读取该文件下的数据,用 ...

  6. [转]DICOM医学图像处理:Deconstructed PACS之Orthanc

    转载:http://blog.csdn.net/zssureqh/article/details/41424027 背景: 此篇博文介绍一个开源的.基于WEB的DICOM Server软件.该开源软件 ...

  7. [置顶] JDK工具(一)–Java编译器javac

    1.概述    javac.exe: Java编译器,将Java源代码转换成字节码. 2.用法    javac <选项> <源文件> (使用过程中发现,javac <源 ...

  8. jQuery序列化表单数据 serialize()、serializeArray()及使用

    1.serialize() 方法: serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. 您可以选择一个或多个表单元素(比如 input 及/或 文本框),或者 form 元素 ...

  9. .NET-使用NPOI组件将数据导出Excel-通用方法

    一.Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题:导出问题: 如果是asp.net,你 ...

  10. 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

    原文:https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec105 ...