Usage
-
NotificationManager类
- 获取
NotificationManager
对象,通过Content.getSystemService
方法:
1. 获取对象方法
NotificationManager manager=(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
2.源码实现
public static NotificationManager from(Context context) {
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
2. 通知Notification类在通知栏显示通知,通过` NotificationManager.notify() `方
1. 使用方法
manager.notify(1,notification);
2. 源码实现
/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
* @param id An identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing what to show the user. Must not
* be null.
*/
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}
-
Notification类
- 构造
Notification
,通过Notification.Builder
或者NotificationCompat.Builder
方法
1. 关于 `Notification` 的构造函数,新版本的 ** android API ** 已经不推荐使用构造函数来生成 `Notification` 对象了:
/*** Constructs a Notification object with default values.
* You might want to consider using {@link Builder} instead.
*/
public Notification()
{
this.when = System.currentTimeMillis();
this.priority = PRIORITY_DEFAULT;
}
/**
* @hide
*/
public Notification(Context context, int icon, CharSequence tickerText, long when,
CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
{
new Builder(context)
.setWhen(when)
.setSmallIcon(icon)
.setTicker(tickerText)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
.buildInto(this);
}
@Deprecated
public Notification(int icon, CharSequence tickerText, long when)
{
this.icon = icon;
this.tickerText = tickerText;
this.when = when;
}
>
2. `Notification.setLatestEventInfo`()也被新版本的API给废弃了
* @deprecated Use {@link Builder} instead.
* @removed
*/
@Deprecated
public void setLatestEventInfo(Context context,
CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Notification.Builder builder = new Notification.Builder(context);
// First, ensure that key pieces of information that may have been set directly
// are preserved
builder.setWhen(this.when);
builder.setSmallIcon(this.icon);
builder.setPriority(this.priority);
builder.setTicker(this.tickerText);
builder.setNumber(this.number);
builder.setColor(this.color);
builder.mFlags = this.flags;
builder.setSound(this.sound, this.audioStreamType);
builder.setDefaults(this.defaults);
builder.setVibrate(this.vibrate);
builder.setDeleteIntent(this.deleteIntent);
// now apply the latestEventInfo fields
if (contentTitle != null) {
builder.setContentTitle(contentTitle);
}
if (contentText != null) {
builder.setContentText(contentText);
}
builder.setContentIntent(contentIntent);
builder.buildInto(this);
}
3. 新版本的API生成 `Notification` 对象
- 先生成` Builder `对象B
Notification.Builder builder = new Notification.Builder(this);
或者
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
- 设置 `Builder` 对象的参数
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle("普通通知");
- 再通过`Builder`的成员函数`Builder.build()`来生成 `Notification` 对象
Notification notification= builder.build();
- 通常生成 `Notification` 对象,如下:
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
代码实现:
**MainActivity.java**
package com.fd.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send_noti=(Button) findViewById(R.id.send_noti);
send_noti.setOnClickListener(send_notiListener);
}
//实现点击按钮发送通知操作
private Button.OnClickListener send_notiListener=new Button.OnClickListener(){
public void onClick(View v){
send_NotiToReceiver();
}
};
//发送通知
private void send_NotiToReceiver(){
NotificationManager notiManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notiMessage=buildNoti("Click goto NotiActivity","Noti");
loadNotiConfig(notiMessage);
notiManager.notify(0,notiMessage);
}
//生成通知内容
private Notification buildNoti(String NotiText,String NotiTitle){
Notification.Builder builder=new Notification.Builder(this);
PendingIntent pIntent=buildPendingIntent();
builder.setContentIntent(pIntent);
builder.setContentText(NotiText);
builder.setContentTitle(NotiTitle);
builder.setSmallIcon(R.mipmap.noti_iocn);
builder.setAutoCancel(true);
builder.setWhen(System.currentTimeMillis());
return builder.build();
}
//加载声音、震动、提示灯等
private void loadNotiConfig(Notification notification){
notification.defaults |=Notification.DEFAULT_LIGHTS;
notification.defaults |=Notification.DEFAULT_SOUND;
notification.defaults |=Notification.DEFAULT_VIBRATE;
}
//生成PendingIntent对象
private PendingIntent buildPendingIntent(){
Intent intent=new Intent(this,NotiActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(this,0,intent,0);
return pIntent;
}
}