发送自定义通知
package com.example.day03;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendPro();
}
private void send() {
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("头部").setContentText("内容");
Notification build = builder.build();
NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
systemService.notify(1, build);
}
});
}
}
发送进度条通知
private void sendPro() {
final NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.mipmap.ic_launcher_round);
builder.setContentTitle("标题");
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int index = 0;
@Override
public void run() {
builder.setContentText("正在下载");
builder.setProgress(100, index, false);
index++;
systemService.notify(2, builder.build());
if (index == 100) {
builder.setContentText("正在安装");
builder.setProgress(0,0,true);
systemService.notify(2,builder.build());
try {
Thread.sleep(0b1001110001000);
} catch (InterruptedException e) {
e.printStackTrace();
}
systemService.cancel(2);
timer.cancel();
}
}
}, 0, 100);
}
可交互式通知
Notification.Builder builder = new Notification.Builder(MainActivity.this);
NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
PendingIntent activity = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.mipmap.ic_launcher_round);
builder.setContentText("我的内容");
builder.setContentTitle("我的标题");
builder.setContentIntent(activity);
systemService.notify(1, builder.build());
Notification通知效果
private void showNotifyOnlyText() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(mLargeIcon)
.setContentTitle("我是只有文字效果的通知")
.setContentText("我没有铃声、震动、呼吸灯,但我就是一个通知");
mManager.notify(1, builder.build());
}
private void showNotifyWithRing() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有铃声效果的通知")
.setContentText("美妙么?安静听~")
.setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));
mManager.notify(2, builder.build());
}
private void showNotifyWithVibrate() {
long[] vibrate = new long[]{0, 500, 1000, 1500};
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有震动效果的通知")
.setContentText("颤抖吧,凡人~")
.setVibrate(vibrate);
mManager.notify(3, builder.build());
}
private void showNotifyWithLights() {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是带有呼吸灯效果的通知")
.setContentText("一闪一闪亮晶晶~")
.setLights(0xFF0000, 3000, 3000);
Notification notify = builder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mManager.notify(4, builder.build());
}
}, 10000);
}
private void showNotifyWithMixed() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
.setContentText("我是最棒的~")
.setDefaults(Notification.DEFAULT_ALL);
mManager.notify(5, builder.build());
}
private void showInsistentNotify() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是一个死循环,除非你取消或者响应")
.setContentText("啦啦啦~")
.setDefaults(Notification.DEFAULT_ALL);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_INSISTENT;
mManager.notify(6, notify);
}
private void showAlertOnceNotify() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("仔细看,我就执行一遍")
.setContentText("好了,已经一遍了~")
.setDefaults(Notification.DEFAULT_ALL);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mManager.notify(7, notify);
}
private void clearNotify() {
mManager.cancelAll();