<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:text="普通通知"
android:onClick="normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="分组通知"
android:onClick="groupNotify"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="进度条通知"
android:onClick="progressNotify"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="自定义通知"
android:onClick="divNotify"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.bawei.day3_notificationdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.NotificationTarget;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//普通通知
public void normal(View view){
//获取通知管理器,因为是一个系统的服务,所以需要强转
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//获取通知的对象,是一个builder,链式编程结构,创建者模式
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);//设置通知小图标,一定要设置
builder.setContentTitle("通知");//标题
builder.setContentText("我是普通消息通知");//内容
builder.setAutoCancel(true);//设置是否删除
//builder.setLargeIcon();//设置大图标
//设置通知效果Notification.DEFAULT_ALL代表全部,Notification.DEFAULT_LIGHTS灯光Notification.DEFAULT_SOUND声音Notification.DEFAULT_VIBRATE震动
builder.setDefaults(Notification.DEFAULT_ALL);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,101,intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);//设置intent,可以使通知跳转
Notification notification = builder.build();
//发送通知
notificationManager.notify(1,notification);//或报错,记得加通知的权限
}
//分组通知
public void groupNotify(View view){
//获取通知管理器,因为是一个系统的服务,所以需要强转
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//获取通知对象,是一个builder,链式编程结构,创建者模式
Notification builder1 = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知1")
.setContentText("通知1内容")
.setGroup("a")//分组通知关键位置,设置该通知的组
.setGroupSummary(true)//是否重叠,不显示,折叠在底部
.build();
Notification builder2 = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知2")
.setContentText("通知2内容")
.setGroup("a")//分组通知关键位置,设置该通知的组
.setGroupSummary(true)//是否重叠,不显示,折叠在底部
.build();
Notification builder3 = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知3")
.setContentText("通知3内容")
.setGroup("b")//分组通知关键位置,设置该通知的组
.setGroupSummary(true)//是否重叠,不显示,折叠在底部
.build();
Notification builder4 = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知4")
.setContentText("通知4内容")
.setGroup("b")//分组通知关键位置,设置该通知的组
.setGroupSummary(true)//是否重叠,不显示,折叠在底部
.build();
notificationManager.notify(1,builder1);
notificationManager.notify(2,builder2);
notificationManager.notify(3,builder3);
notificationManager.notify(4,builder4);
}
private Timer timer;
//进度条通知,主要需要使用定时器或者线程来更新通知
public void progressNotify(View view){
//获取通知管理器,因为是一个系统的服务,所以需要强转
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("下载")
.setProgress(100,0,false);//参数进度条最大值,当前进度,是否模糊显示进度
notificationManager.notify(6,builder.build());
timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress == 100){
notificationManager.cancel(6);
timer.cancel();
}else {
builder.setProgress(100,progress += 10,false);
notificationManager.notify(6,builder.build());
}
}
},0,1000);
}
//自定义通知
public void divNotify(View view){
//获取通知管理器
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//使用远程View加载布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.diy);
//修改远程的text,给text赋值
remoteViews.setTextViewText(R.id.tv,"鸡你太美");
//加载本地图片,第一个参数是id,第二个参数是需要替换或者加载的本地图片
remoteViews.setImageViewResource(R.id.play,R.drawable.ic_pause);
//设置远程布局中的本地图片
Notification notification = null;
//使用setCustomContentView
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomContentView(remoteViews)///加载远程布局
.build();
}
//发送通知
notificationManager.notify(7,notification);
//加载网络图片
//参数,上下文,需要加载图片的位置,远程视图(remoteViews),当前通知,通知的id
NotificationTarget notificationTarget = new NotificationTarget(this,R.id.iv,remoteViews,notification,7);
Glide.with(this).asBitmap().load("http://43.143.146.165:8080/class5server/image/1.jpg").into(notificationTarget);
}
//双击返回键退出
private long first;//记录第一次点击的时间
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
//找到返回的键值,通过keyCode可以获取当前是否点击的是返回键
//两次点击误差在2秒,认为是连点,可以退出app,否则提示“再点一次,退出app”
long second = System.currentTimeMillis();//第二次点击的时间
if (second - first > 2000){
first = second;//将第二次点击的时间赋值给第一次,以便下一次比较
Toast.makeText(this, "再点一次,退出app", Toast.LENGTH_SHORT).show();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--自定义通知-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:padding="10dp"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:text="歌曲名称"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/previous"
android:src="@drawable/ic_previous"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/play"
android:src="@drawable/ic_play"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/next"
android:src="@drawable/ic_next"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>