展会信息港展会大全

Android学习笔记(一)之Notification
来源:互联网   发布日期:2015-10-02 21:51:06   浏览:1679次  

导读:首先新建一个Android工程然后编辑main.xml代码如下:[xhtml]<?xml version="1.0" encoding="utf-8"?><LinearLayou...

首先新建一个Android工程

然后编辑main.xml

代码如下:

[xhtml]

<?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:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/send"

android:id="@+id/button"

/>

</LinearLayout>

再编辑strings.xml

[xhtml]

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">通知</string>

<string name="app_name">状态栏通知</string>

<string name="send">发送通知</string>

</resources>

然后就是对其Notification进行处理

下面来看一下创建并显示一个Notification的步骤。创建和显 示一个Notification需要如下5步:

1. 通过getSystemService方法获得一个NotificationManager对象。

2. 创建一个Notification对象。每一个Notification对应一个Notification对象。在这一步需要设置显示在屏幕上方状态栏的通知消息、通知消息前方的图像资源ID和发出通知的时间。一般为当前时间。

3. 由于Notification可以与应用程序脱离。也就是说,即使应用程序被关闭,Notification仍然会显示在状态栏 中。当应用程序再次启动后,又可以重新控制这些Notification。如清除或替换它们。因此,需要创建一个PendingIntent对象。该对象由Android系统负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。

4. 使用Notification类的setLatestEventInfo方法设置Notification的详细信息。

5. 使用NotificationManager类的notify方法显示Notification消息。在这一步需要指定标识Notification的唯一ID。这个ID必须相对于同一个NotificationManager对象是唯一的,否则就会覆盖相同ID的Notificaiton。

心动不如行动,下面我们来演练一下如何在状 态栏显示一个Notification,代码如下:

[java]

package com.cayden.notification;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

/**

* @author 崔冉

*

*/

public class NotificationActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button button=(Button)this.findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

/**

* 获取通知管理器

*/

NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

int icon=android.R.drawable.sym_action_email;

long when=System.currentTimeMillis();

/**

* 新建一个通知指定图标和标题

*/

Notification notification=new Notification(icon,null,when);

notification.defaults=Notification.DEFAULT_SOUND;//发出默认声音

PendingIntent contenIntent=PendingIntent.getActivity(NotificationActivity.this, 0, null, 0);

notification.setLatestEventInfo(NotificationActivity.this, "开会通知", "今天下午四点半在512开会!", contenIntent);

notificationManager.notify(0, notification);//发送通知

}

});

}

}

运行效果如图所示:

点击发送通知按钮

后的效果

在左上角多出一个邮件符号.

然后用鼠标选中往下拖

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港