第三单元:双击退出,Notitfcation 通知
双击退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ){
//判断用户两次按键的时间差是否在一个预期值之内,是的话直接直接退出,不是的话提示用户再按一次后退键退出。
if(System.currentTimeMillis() - exitTime > 2000){
Toast.makeText(this,"在点就退出",Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
//当返回true时,表示已经完整地处理了这个事件,并不希望其他的回调方法再次进行处理,而当返回false时,
// 表示并没有完全处理完该事件,更希望其他回调方法继续对其进行处理,
return true;
}else{
finish(); //结束当前activity
}
}
return super.onKeyDown(keyCode, event);
}
发送一个最简单的通知
package com.example.day003;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* 任务栏通知
* 风一样的男人
*/
public class NotificationActivity extends AppCompatActivity {
private Button sendMessageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
sendMessageId = findViewById(R.id.send_message_id);
sendMessageId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//定义一个发送通知的方法
sendNotification();
}
});
}
/**
* 最简单的一个通知
*/
private void sendNotification() {
//创建构造者
Notification.Builder builder = new Notification.Builder(this);
//设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置
builder.setContentTitle("我是标题"); //建议设置
builder.setContentText("我是内容"); //建议设置
// builder.setTicker("我是提示信息");
// builder.setContentInfo("我是附加信息"); //7.0以后已经过期
//创建对象.发送的就是这个对象
Notification build = builder.build();
//获取通知管理器,负责发通知、清除通知等
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//TODO :发送通知
//参数一 id 通知的id(稍后介绍意义) 参数二 通知对象
notificationManager.notify(1,build);
}
}
自定义通知
package com.example.day003;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import java.math.RoundingMode;
/**
* 任务栏通知
* 风一样的男人
*/
public class NotificationActivity extends AppCompatActivity {
private Button sendMessageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
sendMessageId = findViewById(R.id.send_message_id);
sendMessageId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//定义一个最简单通知的方法(无交互)
// sendNotification();
//定义一个有交互的通知的方法
// sendActionNotification();
//自定义一个通知
userNotification();
}
});
}
/**
* 自定义一个通知
*/
private void userNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText("内容");
builder.setContentTitle("头部");
/**
* RemoteViews是可以在别的进程(系统进程)中显示的View,并且提供了一组跨进程更新它界面的操作
* 两个参数,第一个布局所在包名
* 第二个是布局Id
* 布局文件是自己创建的,随便一个线性布局,加一个textView和ImageView即可
*/
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.simple_layout);
/**
* 由于运行在不同的进程中,所以RemoteViews无法像正常的View一样更新UI。
* RemoteViews提供了一系列的set方法,但是这些set方法只是View全部方法的子集。
*/
//都是两个参数,第一个参数相当于findViewById,第二个是设置一个值.
remoteViews.setTextViewText(R.id.rm_text_id,"十七岁的老冯");
remoteViews.setImageViewResource(R.id.rm_image_id,R.mipmap.ic_launcher);
//builder.setContent(remoteViews);//过期
builder.setCustomContentView(remoteViews)
Notification build = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(2,build);
}
进度条通知
//进度条通知
private void progress_notification() {
final NotificationManager manager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
//TODO 2:创建构造者
final Notification.Builder builder = new Notification.Builder(this);
//TODO 3:设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentTitle("我是标题");
//TODO 设置进度条
//参数一 最大值 参数二:当前进度 参数三 是否模糊
// builder.setProgress(100,50,true);
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
int progress;
@Override
public void run() {
//1.模拟下载过程
builder.setContentText("正在下载,当前进度"+progress);
builder.setProgress(100,progress,false);//确定的进度条
progress+=10;
manager.notify(6,builder.build());
if(progress==100){
//2.安装过程
builder.setContentText("正在安装");
builder.setProgress(0,0,true);//安装模糊
manager.notify(6,builder.build());
try {
Thread.sleep(7000);//模拟安装过程
} catch (InterruptedException e) {
e.printStackTrace();
}
//3.安装完成
manager.cancel(6);//取消置顶的通知
timer.cancel();
}
}
}, 0, 1000);
}
通知分组
private void groupNotification() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder mBuilder0 = new Notification.Builder(this);
Notification.Builder mBuilder1 = new Notification.Builder(this);
Notification.Builder mBuilder2 = new Notification.Builder(this);
Notification.Builder mBuilder3 = new Notification.Builder(this);
//消息分组属性,group相同才能分到一组
mBuilder0.setGroup("2");
mBuilder0.setSmallIcon(R.mipmap.ic_launcher);
//被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息.
mBuilder0.setGroupSummary(true);
mBuilder0.setContentText("11111111");
mBuilder0.setContentTitle("222222222");
mBuilder1.setGroup("2");
mBuilder1.setSmallIcon(R.mipmap.ic_launcher);
// mBuilder1.setGroupSummary(true);
mBuilder1.setContentText("333333");
mBuilder1.setContentTitle("44444444");
mBuilder2.setGroup("2");
mBuilder2.setSmallIcon(R.mipmap.ic_launcher);
// mBuilder2.setGroupSummary(true);
mBuilder2.setContentText("55555555555555555");
mBuilder2.setContentTitle("66666666666");
mBuilder3.setGroup("2");
mBuilder3.setSmallIcon(R.mipmap.ic_launcher);
// mBuilder3.setGroupSummary(true);
mBuilder3.setContentText("77777");
mBuilder3.setContentTitle("8888888");
manager.notify(0,mBuilder0.build());
manager.notify(1,mBuilder1.build());
manager.notify(2,mBuilder2.build());
manager.notify(3,mBuilder3.build());
}
锁屏通知
//builde的时候
//通过 setVisibility() 方法设置即可
.setVisibility(VISIBILITY_PUBLIC)
.build();
安卓7.0直接回复通知
private void responseNotification2() {
// 其中RESULT_KEY是获取回复的内容,setLabel设置的值就是EditText的hint值
RemoteInput remoteInput = new RemoteInput.Builder("KEY").setLabel("回复通知").build();
Intent intent = new Intent(this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher,"回复",pendingIntent).addRemoteInput(remoteInput).build();
// 其中这个Builder需要传递四个参数,第一个就是logo图片,第二个类似于标签我们要点击的。第三个就是要做的动作intent.最后把我们创建的remoteInput加入进来。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"1")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("请问需要银行贷款吗?")
.setContentText("您好,我是XX银行的XX经理, 请问你需要办理银行贷款吗?")
.setColor(Color.CYAN)
.setPriority(Notification.PRIORITY_MAX) // 设置优先级为Max,则为悬浮通知
.addAction(action) // 设置回复action
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL) // 想要悬浮出来, 这里必须要设置
.setCategory(Notification.CATEGORY_MESSAGE);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// NotificationManager nm = getSystemService(NotificationManager.class);
Notification notification = builder.build();
nm.notify(1,notification);
----------------------------------------------------
从这里开始,以下方法在service中其中,以后课程会讲到.
Bundle resultsFromIntent = RemoteInput.getResultsFromIntent(intent);
Log.i(TAG, "responseNotification2: "+resultsFromIntent);
//根据key拿回复的内容
Log.i(TAG, "responseNotification2: ddd");
if (null!=resultsFromIntent){
String resultString = resultsFromIntent.getString("KEY");
//处理回复内容
Log.i(TAG, "responseNotification2: ");
reply(resultString);
}
}
private void reply(final String resultString) {
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(1000);
Log.i(TAG, "run: "+Main2Activity.class.getSimpleName()+resultString);
onReply();
}
}).start();
}
private void onReply() {
final NotificationManager nm = getSystemService(NotificationManager.class);
final Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
// 更新通知为“回复成功”
Notification notification = new NotificationCompat.Builder(NotificationActivity.this,"1")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("回复成功")
.build();
nm.notify(1, notification);
}
});
// 最后将通知取消
handler.postDelayed(new Runnable() {
@Override
public void run() {
nm.cancel(1);
}
}, 2000);
}
通知的样式
/**
* 通知的样式
*/
private void notificationStyle() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("列表通知");
// builder.setContentTitle("大图通知");
//通知内容为大图片
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
//通知内容为列表显示
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.addLine("李白");
inboxStyle.addLine("猴子");
inboxStyle.addLine("露娜");
// builder.setStyle(bigPictureStyle);
builder.setStyle(inboxStyle);
//不能跨APP
Intent intent = new Intent(this, MainActivity.class);
//intent - PendingIntent
PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setFullScreenIntent(intent1, true);
builder.setContentIntent(intent1);
manager.notify(9, builder.build());
}
PendingIntent
//获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags);
//获取一个用于启动 Service 的 PendingIntent 对象
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags);
//获取一个用于向 BroadcastReceiver 广播的 PendingIntent 对象
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
发送一个带交互的通知
package com.example.day003;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* 任务栏通知
* 风一样的男人
*/
public class NotificationActivity extends AppCompatActivity {
private Button sendMessageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
sendMessageId = findViewById(R.id.send_message_id);
sendMessageId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//定义一个最简单通知的方法(无交互)
// sendNotification();
//定义一个有交互的通知的方法
sendActionNotification();
}
});
}
/**
* 可交互
*/
private void sendActionNotification() {
Intent intent = new Intent(this, Main2Activity.class);
/**
* 可简单一个延时的intent 当点击通知栏的信息时,才发送intent
* 第一个参数是上下文
* 第二个参数是 请求码,多个请求码不一样即可
* 第三个参数是 intent
* 第四个参数是 flags 可写0;
*/
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("我是标题");
builder.setContentText("我是内容");
// builder.setAutoCancel(true); //点击消息通知取消
//设置意图对象
builder.setContentIntent(activity);
Notification notification = builder.build();
//构造管理者
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification);
}
}
设置 Notification 的通知效果 (震动需要真机)
// 在builder的时候加上如下属性即可.
builder.setDefaults(Notification.DEFAULT_ALL);
1
2
//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_VIBRATE
//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND
//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS
//添加上述三种默认提醒效果
Notification.DEFAULT_ALL
更新 Notification
更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。
取消 Notification
取消通知有如下 5 种方式:
点击通知栏的清除按钮,会清除所有可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
如果你是通过 NotificationManager.notify(String tag, int id, Notification notify) 方法创建的通知,那么只能通过 NotificationManager.cancel(String tag, int id) 方法才能清除对应的通知,调用NotificationManager.cancel(int id) 无效。
安卓8.0以上通知的写法

2835

被折叠的 条评论
为什么被折叠?



