8.0 Notification的使用1. 普通的notification 2. 帶進度條的notification 3. 自定義的notification,點選事件進入軟體的一個activity
阿新 • • 發佈:2018-12-13
實現功能:
最終效果:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/showNormalNotification" android:text="傳送 普通 通知" android:onClick="showNotification" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/showProgressNotification" android:text="傳送 帶進度條 通知" android:onClick="showNotification" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/showCustomNotification" android:text="傳送 自定義 通知" android:onClick="showNotification" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
activity_notification.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".NotificationActivity"> <TextView android:text="我是自定義的" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.constraint.ConstraintLayout>
layout_custom_notification.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/turn" android:orientation="vertical"> <TextView android:id="@+id/notification_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="notification_title" /> <TextView android:id="@+id/notification_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:text="notification_content" /> </LinearLayout>
MainActivity.java
package com.example.tnt.notificationchannel;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import static android.app.Notification.VISIBILITY_SECRET;
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showNotification(View view) {
switch (view.getId())
{
case R.id.showNormalNotification:
sendNormalNotification();
break;
case R.id.showProgressNotification:
sendProgressNotification();
break;
case R.id.showCustomNotification:
sendCustomNotification();
break;
}
}
// 3. ------------自定義 佈局 和 互動
private void sendCustomNotification(){
Notification.Builder builder = getNotificationBuilder();
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.layout_custom_notification);
remoteViews.setTextViewText(R.id.notification_title,"魔道祖師官方");
remoteViews.setTextViewText(R.id.notification_content,"魏無羨和藍忘機結婚了,喵喵喵"); // 如果不自己在這設定,就會預設佈局裡的設定
Intent intent = new Intent(this,NotificationActivity.class);
// 即將發生的意圖,可以被取消可以被更新。
PendingIntent pendingIntent = PendingIntent.getActivity(this,-1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.turn,pendingIntent);
// 耗資源的意圖
// remoteViews.setOnClickFillInIntent(R.id.turn,intent);
builder.setCustomContentView(remoteViews);
//
getManager().notify(3,builder.build());
}
// 2. ---------- 進度條的Notification-------------
private void sendProgressNotification() {
final Notification.Builder builder = getNotificationBuilder();
builder.setOnlyAlertOnce(true); // 只提示一次聲音
getManager().notify(2, builder.build());
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
builder.setProgress(100, i, false);
getManager().notify(2, builder.build());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
// 管理者 是否存在
private NotificationManager getManager() {
if (manager == null) {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return manager;
}
// 傳送普通通知
private void sendNormalNotification() {
Notification.Builder builder = getNotificationBuilder();
getManager().notify(1, builder.build());
}
// 檢驗 相容性檢驗 是否是 Android 8.0以上
private Notification.Builder getNotificationBuilder() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
channel.canBypassDnd(); //可否繞過勿擾模式
channel.enableLights(true); // 能否閃光
channel.setLockscreenVisibility(VISIBILITY_SECRET); // 鎖屏顯示通知
channel.setLightColor(Color.RED); // 閃光燈顏色
channel.canShowBadge(); // 桌面 lanucher 訊息角標
channel.enableVibration(true); // 能否震動
channel.getAudioAttributes();// 獲取系統響鈴聲音的配置
channel.getGroup(); // 獲取通知取到道組
channel.setBypassDnd(true); // 可以繞過勿打擾模式
channel.setVibrationPattern(new long[]{100, 100, 100}); // 震動的時間間隔
channel.shouldShowLights(); // 是否閃光
getManager().createNotificationChannel(channel); // 相當於 manger.createNotificationChannel(channel);
}
return new Notification.Builder(this)
.setAutoCancel(true)
.setChannelId("channel_id")
.setContentTitle("每日新聞")
.setContentText("明天是週六")
.setSmallIcon(R.mipmap.ic_launcher_round)
// .setStyle(new Notification.BigTextStyle())
;
}
}
NotificationActivity.java
package com.example.tnt.notificationchannel;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NotificationActivity extends AppCompatActivity {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
// 建立之後清除 通知
getManager().cancel(3);
}
//1.-----------
private NotificationManager getManager() {
if (manager == null) {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return manager;
}
}