展会信息港展会大全

Android技术精髓-Notification Activity
来源:互联网   发布日期:2015-10-02 21:46:58   浏览:1401次  

导读:Android技术精髓-Notification ActivityNotifications有过Android开发经验的朋友都应该知道,Notification 是可以显示给用户应...

Android技术精髓-Notification Activity

Notifications

有过Android开发经验的朋友都应该知道,Notification 是可以显示给用户应用程序的正常的UI之外。当系统发出通知,它出现在通知区域中的图标。用户可以打开 notification drawer看该通知的详细信息。

同时手机会有如下相应:

1、首先状态栏,通知区域持久的图标

2、开启或闪烁device‘s LED

3、通过闪烁的背光,播放声音或震动提醒用户

notificationActivity代码:

代码功能是在Activity中通过点击button 开启一个task,延迟10秒插入消息列队开启一个notification!

package activitys;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class NotificationActivity extends Activity implements View.OnClickListener {

private static final int NOTE_ID = 100;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Button button = new Button(this);

button.setText("Post New Notification");

button.setOnClickListener(this);

setContentView(button);

}

@Override

public void onClick(View v) {

handler.postDelayed(task, 10000);

Toast.makeText(this, "Notification will post in 10 seconds",

Toast.LENGTH_SHORT).show();

}

private Handler handler = new Handler();

private Runnable task = new Runnable() {

@Override

public void run() {

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

Intent launchIntent = new Intent(getApplicationContext(),

Test.class);

PendingIntent contentIntent = PendingIntent.getActivity(

getApplicationContext(), 0, launchIntent, 0);

Notification note = new Notification(R.drawable.icon,

"Something Happened", System.currentTimeMillis());

note.setLatestEventInfo(getApplicationContext(), "Finished!",

"Click Here!", contentIntent);

note.defaults |= Notification.DEFAULT_SOUND;

note.flags |= Notification.FLAG_AUTO_CANCEL;

manager.notify(NOTE_ID, note);

}

};

}

postDelayed(Runnable r, long delayMillis)

延时delayMillis毫秒 将Runnable插入消息列队,Runnable将在handle绑定的线程中运行。

post 是立即插入消息列队,当消息列队处理到该消息时才运行

PendingIntent

我们知道Intent 是及时启动,intent 随所在的activity 消失而消失。

PendingIntent 是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例

pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent,

就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。

另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。

Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。

总而言之,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。

Notification

通知类Notification

public class

notification

extends Object

implements Parcelable

1、创建Notification

通过NotificationManager的notify(int,Notification)方法来启动Notification。

第一个参数唯一的标识该Notification,第二个参数就是Notification对象。

2、更新Notification

调用Notification的setLatestEventInfo方法来更新内容,然后再调用NotificationManager的notify()方法即可。(具体可以看下面的实例)

3、删除Notification

通过NotificationManager的cancel(int)方法,来清除某个通知。其中参数就是Notification的唯一标识ID。

当然也可以通过cancelAll()来清除状态栏所有的通知。

4、Notification设置(振动、铃声等) Notification示例代码:

//新建状态栏通知

baseNF=newNotification();

//设置通知在状态栏显示的图标

baseNF.icon=R.drawable.icon;

//通知时在状态栏显示的内容

baseNF.tickerText="YouclickedBaseNF!";

//通知的默认参数DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_LIGHTS.

//如果要全部采用默认值,用DEFAULT_ALL.

//此处采用默认声音

baseNF.defaults=Notification.DEFAULT_SOUND;

//第二个参数:下拉状态栏时显示的消息标题expandedmessagetitle

//第三个参数:下拉状态栏时显示的消息内容expandedmessagetext

//第四个参数:点击该通知时执行页面跳转

baseNF.setLatestEventInfo(Lesson_10.this,"Title01","Content01",pd);

//发出状态栏通知

//ThefirstparameteristheuniqueIDfortheNotification

//andthesecondistheNotificationobject.

nm.notify(Notification_ID_BASE,baseNF);

添加Notification声音:

1、如果要采用默认声音,只要使用default就可以了。

baseNF.defaults=Notification.DEFAULT_SOUND;

2、如果要使用自定义声音,那么就要用到sound了。如下:

notification.sound=Uri.parse("file:///sdcard/notification/ringer.mp3");

3、如果想用系统自带的铃声,可以这样:

notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"6");

(如果default、sound同时出现,那么sound无效,会使用默认铃声。 )

4、默认情况下,通知的声音播放一遍就会结束。如果你想让声音循环播放,需要为flags参数加上FLAG_INSISTENT。这样声音会到用户响应才结束,比如下拉状态栏。notification.flags|=notification.FLAG_INSISTENT;

Notification添加振动 :

1、默认的振动方式。

notification.defaults|=Notification.DEFAULT_VIBRATE;

2、自己定义振动形式,这边需要用到Long型数组。

long[]vibrate={0,100,200,300};

notification.vibrate=vibrate;

Long型数组中,第一个参数是开始振动前等待的时间,第二个参数是第一次振动的时间,第三个参数是第二次振动的时间,以此类推,随便定义多长的数组。但是采用这种方法,没有办法做到重复振动。

同样,如果default、vibrate同时出现时,会采用默认形式。 注意添加权限:

Notification添加闪光:

1、默认的灯光

notification.defaults|=Notification.DEFAULT_LIGHTS;

2、自定义灯光:notification.ledARGB=0xff00ff00;

notification.ledOnMS=300;

notification.ledOffMS=1000;

notification.flags|=Notification.FLAG_SHOW_LIGHTS; 其中ledARGB表示灯光颜色、ledOnMS亮持续时间、ledOffMS暗的时间。 Notification其他有用的设置: flags:

Notification.FLAG_INSISTENT;//让声音、振动无限循环,直到用户响应

Notification.FLAG_AUTO_CANCEL;//通知被点击后,自动消失

Notification.FLAG_NO_CLEAR;//点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个

//自定义下拉视图,比如下载软件时,显示的进度条。

Notificationnotification=newNotification();

notification.icon=R.drawable.icon;

notification.tickerText="Custom!";

RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom);

contentView.setImageViewResource(R.id.image,R.drawable.icon);

contentView.setTextViewText(R.id.text,"Hello,thismessageisinacustomexpandedview");

notification.contentView=contentView;

//使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法

//但是必须定义contentIntent

notification.contentIntent=pd;

nm.notify(3,notification);

使用自定义的Notification

要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给contentIntent字段。以下示例代码是完整步骤:

1、创建一个自定义的消息布局 view.xml

//2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);

contentView.setImageViewResource(R.id.image,R.drawable.icon);

contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”);

notification.contentView = contentView;

//3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//4、发送通知

mNotificationManager.notify(2,notification);

//以下是全部示例代码

//创建一个NotificationManager的引用

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

//定义Notification的各种属性

int icon = R.drawable.icon; //通知图标

CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

//用上面的属性初始化Nofification

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

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);

contentView.setImageViewResource(R.id.image, R.drawable.iconempty);

contentView.setTextViewText(R.id.text, "Hello,this is JC");

notification.contentView = contentView;

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//把Notification传递给NotificationManager

mNotificationManager.notify(0,notification);

2014.1.22

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

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