Notification
notification是在系统的通知栏中呈现多样式持久性消息的类。
- 在通知栏显示
- 消息持久性
- 种类多样性
notification的继承关系
image普通通知
Java代码
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notification(View view) {
//获得通知的管理对象
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//通知的构建对象
Notification.Builder builder = new Notification.Builder(this);
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置大图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
//设置标题
builder.setContentTitle("我是通知的标题");
//设置通知的正文
builder.setContentText("我是通知的正文");
//设置通知的附加消息
builder.setContentInfo("我是通知的附加消息");
//设置通知点击关闭
builder.setAutoCancel(true);
//设置震动,呼吸灯闪,声音
builder.setDefaults(Notification.DEFAULT_ALL);
//设置通知的点击意图 跳转到系统设置界面
Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
//PendingIntent 等待未决定的intent
PendingIntent activity = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
//设置intent
builder.setContentIntent(activity);
//创建出来通知
Notification build = builder.build();
/***
*设置给通知管理对象 -->发送通知
* 同一个id的通知会被认为是同一条通知
* 后发的通知会替换原来的通知
*/
notificationManager.notify(200,build);
}
}
效果图
image进度条通知
Java代码
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private int progress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ProgressNotifcation(View view) {
//获得通知的管理对象
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//通知的构建对象
final Notification.Builder builder = new Notification.Builder(this);
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置大图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
//设置标题
builder.setContentTitle("进度条通知");
//设置文本内容
builder.setContentText("这是一个进度条通知");
/***
*设置给通知管理对象 -->发送通知
* 同一个id的通知会被认为是同一条通知
* 后发的通知会替换原来的通知
*/
notificationManager.notify(200,builder.build());
new CountDownTimer(11000,1000){
@Override
public void onTick(long millisUntilFinished) {
//增加进度
progress+=10;
/***
* 设置进度条
* max 最大进度
* progress 当前进度
* indeterminate 是否是模糊进度条
*/
builder.setProgress(100,progress,false);
//再次发送通知 并覆盖掉原来的通知
notificationManager.notify(200,builder.build());
}
@Override
public void onFinish() {
//结束指定通知
notificationManager.cancel(200);
//结束所有通知
//notificationManager.cancelAll();
}
}.start();
}
}
效果图
image大图通知
Java代码
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void big(View view) {
//获得通知的管理对象
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//通知的构建对象
final Notification.Builder builder = new Notification.Builder(this);
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置大图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
//设置标题
builder.setContentTitle("大图通知");
//设置文本内容
builder.setContentText("这是一个大图通知");
//用于生成包含大型图像附件的大格式通知的
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
//用于生成包含大量文本的大格式通知的
// Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
//用于生成包含(最多5个)字符串列表的大格式通知
// Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
//设置图片
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
//设置通知的风格
builder.setStyle(bigPictureStyle);
/***
*设置给通知管理对象 -->发送通知
* 同一个id的通知会被认为是同一条通知
* 后发的通知会替换原来的通知
*/
notificationManager.notify(200,builder.build());
}
}
效果图
image列表通知
Java代码
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notification(View view) {
//获得通知的管理对象
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//通知的构建对象
Notification.Builder builder = new Notification.Builder(this);
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置大图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
//设置标题
builder.setContentTitle("列表通知");
//设置通知的正文
builder.setContentText("列表通知的正文");
//设置通知的附加消息
builder.setContentInfo("列表通知的附加消息");
//设置通知点击关闭
builder.setAutoCancel(true);
//设置震动,呼吸灯闪,声音
builder.setDefaults(Notification.DEFAULT_ALL);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
//添加每一行的条目
inboxStyle.addLine("风继续吹");
inboxStyle.addLine("沉默是金");
inboxStyle.addLine("有谁共鸣");
inboxStyle.addLine("无心睡眠");
inboxStyle.addLine("洁身自好");
//设置通知条的样式
builder.setStyle(inboxStyle);
/***
*设置给通知管理对象 -->发送通知
* 同一个id的通知会被认为是同一条通知
* 后发的通知会替换原来的通知
*/
// 设置通知的点击意图 跳转到系统设置界面
Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
PendingIntent activity = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
//设置悬挂
builder.setFullScreenIntent(activity, true);
//设置intent
builder.setContentIntent(activity);
//创建出来通知
Notification build = builder.build();
notificationManager.notify(200, build);
}
}
效果图
image自定义通知
Java代码
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@RequiresApi(api = Build.VERSION_CODES.N)
public void notification(View view) {
//获得通知的管理对象
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//通知的构建对象
Notification.Builder builder = new Notification.Builder(this);
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置大图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
//设置标题
builder.setContentTitle("这是自定义的通知");
/***
* 获取要显示的布局
* public RemoteViews(String 包名, int 要显示的布局) {}
*/
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification_layout);
//设置文字
remoteViews.setTextViewText(R.id.textView,"这是一条自定义通知");
//设置文字颜色
remoteViews.setTextColor(R.id.textView, Color.BLACK);
//设置图片
remoteViews.setImageViewResource(R.id.imageView,R.mipmap.ic_launcher);
//7以下
builder.setContent(remoteViews);
//7以上
// builder.setCustomContentView(remoteViews);
notificationManager.notify(2,builder.build());
}
}
效果图
image
网友评论