重构通知栏模块
This commit is contained in:
parent
df6633046c
commit
6555346618
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Tue May 06 11:34:02 HKT 2025
|
#Tue May 06 09:02:43 GMT 2025
|
||||||
stageCount=3
|
stageCount=3
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.0
|
baseVersion=15.0
|
||||||
publishVersion=15.0.2
|
publishVersion=15.0.2
|
||||||
buildCount=0
|
buildCount=2
|
||||||
baseBetaVersion=15.0.3
|
baseBetaVersion=15.0.3
|
||||||
|
@ -120,7 +120,7 @@ public class NotificationHelper {
|
|||||||
PendingIntent pendingIntent = createPendingIntent(intent);
|
PendingIntent pendingIntent = createPendingIntent(intent);
|
||||||
|
|
||||||
|
|
||||||
Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMPORARY)
|
Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID_FOREGROUND)
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
.setSmallIcon(R.drawable.ic_launcher)
|
||||||
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
|
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
|
||||||
//.setContentTitle(title)
|
//.setContentTitle(title)
|
||||||
|
@ -5,16 +5,12 @@ package cc.winboll.studio.timestamp.utils;
|
|||||||
* @Date 2025/05/05 21:10
|
* @Date 2025/05/05 21:10
|
||||||
* @Describe TimeStampRemoteViewsUtil
|
* @Describe TimeStampRemoteViewsUtil
|
||||||
*/
|
*/
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
import android.widget.TextView;
|
|
||||||
import androidx.core.app.NotificationCompat;
|
|
||||||
import cc.winboll.studio.timestamp.MainActivity;
|
import cc.winboll.studio.timestamp.MainActivity;
|
||||||
import cc.winboll.studio.timestamp.R;
|
import cc.winboll.studio.timestamp.R;
|
||||||
import cc.winboll.studio.timestamp.receivers.ButtonClickReceiver;
|
import cc.winboll.studio.timestamp.receivers.ButtonClickReceiver;
|
||||||
@ -27,13 +23,16 @@ public class TimeStampRemoteViewsUtil {
|
|||||||
|
|
||||||
static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil;
|
static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil;
|
||||||
Context mContext;
|
Context mContext;
|
||||||
|
NotificationHelper mNotificationHelper;
|
||||||
RemoteViews mRemoteViews;
|
RemoteViews mRemoteViews;
|
||||||
TextView mtvMessage;
|
Intent mIntentMain;
|
||||||
Notification mNotification;
|
|
||||||
|
|
||||||
|
|
||||||
TimeStampRemoteViewsUtil(Context context) {
|
TimeStampRemoteViewsUtil(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
createNotificationChannel();
|
mNotificationHelper = new NotificationHelper(context);
|
||||||
|
//createNotificationChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized TimeStampRemoteViewsUtil getInstance(Context context) {
|
public static synchronized TimeStampRemoteViewsUtil getInstance(Context context) {
|
||||||
@ -43,24 +42,67 @@ public class TimeStampRemoteViewsUtil {
|
|||||||
return _TimeStampRemoteViewsUtil;
|
return _TimeStampRemoteViewsUtil;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createNotificationChannel() {
|
// private void createNotificationChannel() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
CharSequence name = "自定义视图通知通道";
|
// CharSequence name = "自定义视图通知通道";
|
||||||
String description = "用于展示自定义视图的通知通道";
|
// String description = "用于展示自定义视图的通知通道";
|
||||||
int importance = NotificationManager.IMPORTANCE_HIGH;
|
// int importance = NotificationManager.IMPORTANCE_HIGH;
|
||||||
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
// NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
||||||
channel.setDescription(description);
|
// channel.setDescription(description);
|
||||||
NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
|
// NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
|
||||||
notificationManager.createNotificationChannel(channel);
|
// notificationManager.createNotificationChannel(channel);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// public void showNotification(String msg) {
|
||||||
|
// if (mRemoteViews == null) {
|
||||||
|
// // 创建 RemoteViews 对象,加载布局
|
||||||
|
// mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_layout);
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// // 自定义 TextView 的文本
|
||||||
|
// mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
|
||||||
|
// // 自定义 TextView 的文本颜色
|
||||||
|
// mRemoteViews.setTextColor(R.id.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null));
|
||||||
|
// // 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题)
|
||||||
|
//
|
||||||
|
// // 创建点击通知后的意图
|
||||||
|
// Intent intent = new Intent(mContext, MainActivity.class);
|
||||||
|
// PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
// // 设置通知的点击事件
|
||||||
|
// mRemoteViews.setOnClickPendingIntent(R.id.tv_timestamp, pendingMainIntent);
|
||||||
|
//
|
||||||
|
// // 创建点击按钮后要发送的广播 Intent
|
||||||
|
// Intent broadcastIntent = new Intent(ButtonClickReceiver.BUTTON_COPYTIMESTAMP_ACTION);
|
||||||
|
// android.app.PendingIntent pendingIntent = android.app.PendingIntent.getBroadcast(
|
||||||
|
// mContext,
|
||||||
|
// 0,
|
||||||
|
// broadcastIntent,
|
||||||
|
// android.app.PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// // 为按钮设置点击事件
|
||||||
|
// mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent);
|
||||||
|
//
|
||||||
|
// // 构建通知
|
||||||
|
// NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
|
||||||
|
// .setSmallIcon(android.R.drawable.ic_dialog_info)
|
||||||
|
// .setContent(mRemoteViews)
|
||||||
|
// .setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
|
// .setOngoing(true)
|
||||||
|
// .setAutoCancel(true);
|
||||||
|
//
|
||||||
|
// // 显示通知
|
||||||
|
// NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
|
||||||
|
// mNotification = builder.build();
|
||||||
|
// notificationManager.notify(1, mNotification);
|
||||||
|
// }
|
||||||
|
|
||||||
public void showNotification(String msg) {
|
public void showNotification(String msg) {
|
||||||
if (mRemoteViews == null) {
|
if (mRemoteViews == null) {
|
||||||
// 创建 RemoteViews 对象,加载布局
|
// 创建 RemoteViews 对象,加载布局
|
||||||
mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_layout);
|
mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_layout);
|
||||||
|
|
||||||
}
|
|
||||||
// 自定义 TextView 的文本
|
// 自定义 TextView 的文本
|
||||||
mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
|
mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
|
||||||
// 自定义 TextView 的文本颜色
|
// 自定义 TextView 的文本颜色
|
||||||
@ -68,8 +110,8 @@ public class TimeStampRemoteViewsUtil {
|
|||||||
// 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题)
|
// 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题)
|
||||||
|
|
||||||
// 创建点击通知后的意图
|
// 创建点击通知后的意图
|
||||||
Intent intent = new Intent(mContext, MainActivity.class);
|
mIntentMain = new Intent(mContext, MainActivity.class);
|
||||||
PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, mIntentMain, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
// 设置通知的点击事件
|
// 设置通知的点击事件
|
||||||
mRemoteViews.setOnClickPendingIntent(R.id.tv_timestamp, pendingMainIntent);
|
mRemoteViews.setOnClickPendingIntent(R.id.tv_timestamp, pendingMainIntent);
|
||||||
|
|
||||||
@ -84,18 +126,8 @@ public class TimeStampRemoteViewsUtil {
|
|||||||
|
|
||||||
// 为按钮设置点击事件
|
// 为按钮设置点击事件
|
||||||
mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent);
|
mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent);
|
||||||
|
}
|
||||||
|
|
||||||
// 构建通知
|
mNotificationHelper.showCustomForegroundNotification(mIntentMain, mRemoteViews, mRemoteViews);
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
|
|
||||||
.setSmallIcon(android.R.drawable.ic_dialog_info)
|
|
||||||
.setContent(mRemoteViews)
|
|
||||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
||||||
.setOngoing(true)
|
|
||||||
.setAutoCancel(true);
|
|
||||||
|
|
||||||
// 显示通知
|
|
||||||
NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
|
|
||||||
mNotification = builder.build();
|
|
||||||
notificationManager.notify(1, mNotification);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user