在android开发中经常要使用到通知,比如:收到短息,来电等等,通知是应用程序提醒用户的一种方式,他不需要使用Activity。
通知向用户传递信息有多种方式:(1)状态栏图标 (2)扩展的通知状态绘制器 (3)声音、震动、LED闪烁
通过一个小例子将上面几种方式集成到一起。
在配置扩展状态通知显示的时候,有两种方法:
1,使用setLatestEventInfo方法更新标准的扩展的状态通知显示中所显示的详细信息。
2,使用远程视图(Remote View)设置contentView和contentIntent,这样可以为扩展状态显示分配一个你需要的定制UI.
扩展状态窗口定制布局 my_status.xml(一个简单线性布局,前面放一个imageview,后面一个textview)
要把这个定制布局分配给通知,要创建一个新的RemoteView对象,并把它分配给contentView属性,还需要想contentIntent属性分配一个待处理的意图(Pending Intent)代码如下:
mNotification.contentView = new RemoteViews(this.getPackageName(),R.layout.my_status);
mNotification.contentIntent = mContentIntent;
如果要修改定制布局中视图的属性或者外观,可以使用远程视图对象的set*方法
mNotification.contentView.setImageViewResource(R.id.status_icon,R.drawable.icon);
mNotification.contentView.setTextViewText(R.id.status_text, "This is test content");
向通知添加声音、闪屏、振动效果的最简单最一致的方式是使用当前用户的默认设置。
mNotification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
如果想全部默认设置就使用DEFAULT_ALL常量
如果要使用自己的定制的声音或者动态修改声音就可以设置mNotification.sound = ringURI;
要设置振动类型的话,需要向通知的vibrate属性分配一个longs类型的数组;
比如:
long[] vibreate= new long[]{1000,1000,1000,1000,1000};
mNotification.vibrate = vibreate;
上面代码的作用是振动按照振动1秒,暂停1秒的模式进行振动,整个过程持续5秒。
需要注意的是,使用振动必须添加一个权限:
设置闪屏
mNotification.ledARGB = Color.BLUE;
mNotification.ledOffMS= 0;
mNotification.ledOnMS = 1;
mNotification.flags = mNotification.flags | Notification.FLAG_SHOW_LIGHTS;
ledARGB属性可以用来设置LED的颜色,ledOffMS和ledOnMS属性则可以设置LED闪烁的频率和模式。ledOnMS设置为1并把ledOffMS设置为0来打开LED,两个都设置为0则关闭LED.
完整的代码:
主界面布局文件
activity
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.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class NotificationDemo extends Activity implements OnClickListener{
private Context mContext;
private Button showStatusButton,cancelButton;
private Notification mNotification;
private NotificationManager mNotificationManager;
private final static int NOTIFICATION_ID = 0x0001;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotification = new Notification(R.drawable.icon,"This is a notification.",System.currentTimeMillis());
mNotificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
findViews();
}
public void findViews(){
mContext = NotificationDemo.this;
showStatusButton = (Button)findViewById(R.id.showStatusButton);
cancelButton = (Button)findViewById(R.id.cancelButton);
showStatusButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
public void statusNotifi(){
Intent mIntent = new Intent(mContext,NotificationDemo.class);
//这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent, 0);
//1,使用setLatestEventInfo
//这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.
//mNotification.setLatestEventInfo(mContext, "新消息", "主人,您孙子给你来短息了", mContentIntent);
//2,使用远程视图
mNotification.contentView = new RemoteViews(this.getPackageName(),R.layout.my_status);
mNotification.contentView.setImageViewResource(R.id.status_icon,R.drawable.icon);
mNotification.contentView.setTextViewText(R.id.status_text, "This is test content");
//使用默认的声音,闪屏,振动效果
//mNotification.defaults = Notification.DEFAULT_ALL;
//mNotification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
//添加震动
long[] vibreate= new long[]{1000,1000,1000,1000};
mNotification.vibrate = vibreate;
//添加led
mNotification.ledARGB = Color.BLUE;
mNotification.ledOffMS= 0;
mNotification.ledOnMS = 1;
mNotification.flags = mNotification.flags | Notification.FLAG_SHOW_LIGHTS;
//手动设置contentView属于时,必须同时也设置contentIntent不然会报错
mNotification.contentIntent = mContentIntent;
//触发通知(消息ID,通知对象)
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
public void onClick(View v) {
if(v == showStatusButton){
statusNotifi();
}else if(v == cancelButton){
mNotificationManager.cancel(NOTIFICATION_ID); //取消通知
}
}
}
提示:每个设备对LED的控制方面可能具有不同的限制,如果设置的颜色不可用,可以尝试换用其他颜色试试。
振动和LED在模拟器中是看不到效果的,必须使用真机。