重构常驻通知栏
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Mon May 05 13:36:35 GMT 2025 | ||||
| #Tue May 06 02:08:16 GMT 2025 | ||||
| stageCount=0 | ||||
| libraryProject= | ||||
| baseVersion=15.0 | ||||
| publishVersion=15.0.0 | ||||
| buildCount=91 | ||||
| buildCount=101 | ||||
| baseBetaVersion=15.0.1 | ||||
|   | ||||
| @@ -26,6 +26,10 @@ import cc.winboll.studio.timestamp.utils.AppConfigsUtil; | ||||
| import cc.winboll.studio.timestamp.utils.NotificationHelper; | ||||
| import cc.winboll.studio.timestamp.utils.ServiceUtil; | ||||
| import cc.winboll.studio.timestamp.utils.TimeStampRemoteViewsUtil; | ||||
| import java.time.Instant; | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.ZoneId; | ||||
| import java.time.format.DateTimeFormatter; | ||||
| import java.util.Timer; | ||||
| import java.util.TimerTask; | ||||
|  | ||||
| @@ -106,8 +110,8 @@ public class MainService extends Service { | ||||
|                         mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP); | ||||
|                     } | ||||
|                 }; | ||||
|                 // 延迟2秒后开始执行,之后每隔2000毫秒执行一次 | ||||
|                 mTimer.schedule(task, 2000, 2000); | ||||
|                 // 延迟1秒后开始执行,之后每隔100毫秒执行一次 | ||||
|                 mTimer.schedule(task, 1000, 100); | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -203,8 +207,15 @@ public class MainService extends Service { | ||||
|             switch (message.what) { | ||||
|                 case MSG_UPDATE_TIMESTAMP: | ||||
|                     { | ||||
|                         TimeStampRemoteViewsUtil.getInstance(MainService.this).showNotification("Hello, World"); | ||||
|                         LogUtils.d(TAG, "Hello, World"); | ||||
|                         long currentMillis = System.currentTimeMillis(); | ||||
|                         Instant instant = Instant.ofEpochMilli(currentMillis); | ||||
|                         LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); | ||||
|                         String szTimeStampFormatString = AppConfigsUtil.getInstance(MainService.this).getAppConfigsModel().getTimeStampFormatString(); | ||||
|                         DateTimeFormatter formatter = DateTimeFormatter.ofPattern(szTimeStampFormatString); | ||||
|                         String formattedDateTime = ldt.format(formatter); | ||||
|                         TimeStampRemoteViewsUtil.getInstance(MainService.this).showNotification(formattedDateTime); | ||||
|  | ||||
|                         //LogUtils.d(TAG, "Hello, World"); | ||||
|                         break; | ||||
|                     } | ||||
|                 default: | ||||
|   | ||||
| @@ -15,6 +15,8 @@ import android.widget.RemoteViews; | ||||
| import androidx.core.app.NotificationCompat; | ||||
| import cc.winboll.studio.timestamp.MainActivity; | ||||
| import cc.winboll.studio.timestamp.R; | ||||
| import cc.winboll.studio.timestamp.views.TimeStampView; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| public class TimeStampRemoteViewsUtil { | ||||
|  | ||||
| @@ -25,6 +27,7 @@ public class TimeStampRemoteViewsUtil { | ||||
|     static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil; | ||||
|     Context mContext; | ||||
|     RemoteViews mRemoteViews; | ||||
|     TextView mtvMessage; | ||||
|  | ||||
|     TimeStampRemoteViewsUtil(Context context) { | ||||
|         mContext = context; | ||||
| @@ -54,8 +57,12 @@ public class TimeStampRemoteViewsUtil { | ||||
|         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.custom_text_view, mContext.getResources().getColor(R.color.colorAccent, null)); | ||||
|         mRemoteViews.setTextColor(R.id.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null)); | ||||
|         // 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题) | ||||
|          | ||||
|         // 创建点击通知后的意图 | ||||
| @@ -63,7 +70,7 @@ public class TimeStampRemoteViewsUtil { | ||||
|         PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | ||||
|  | ||||
|         // 设置通知的点击事件 | ||||
|             mRemoteViews.setOnClickPendingIntent(R.id.custom_text_view, pendingIntent); | ||||
|         mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent); | ||||
|  | ||||
|         // 构建通知 | ||||
|         NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID) | ||||
| @@ -75,7 +82,4 @@ public class TimeStampRemoteViewsUtil { | ||||
|         NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class); | ||||
|         notificationManager.notify(1, builder.build()); | ||||
|     } | ||||
|         // 自定义 TextView 的文本 | ||||
|         mRemoteViews.setTextViewText(R.id.custom_text_view, msg); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -87,14 +87,7 @@ public class TimeStampView extends View { | ||||
|  | ||||
| //    public void updateTimeStamp() { | ||||
| //        try { | ||||
| //            long currentMillis = System.currentTimeMillis(); | ||||
| //            Instant instant = Instant.ofEpochMilli(currentMillis); | ||||
| //            LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); | ||||
| //            String szTimeStampFormatString = AppConfigsUtil.getInstance(this.mContext).getAppConfigsModel().getTimeStampFormatString(); | ||||
| //            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(szTimeStampFormatString); | ||||
| //            String formattedDateTime = ldt.format(formatter); | ||||
| //            //System.out.println(formattedDateTime); | ||||
| //            mtvTimeStamp.setText(formattedDateTime); | ||||
| //             | ||||
| //        } catch (Exception e) { | ||||
| //            LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
| //            ToastUtils.show(e); | ||||
|   | ||||
| @@ -1,15 +1,26 @@ | ||||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| <?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="match_parent" | ||||
|     android:orientation="vertical" | ||||
|     android:padding="16dp"> | ||||
| 	android:orientation="horizontal" | ||||
| 	android:padding="16dp" | ||||
| 	android:gravity="center_vertical"> | ||||
|  | ||||
| 	<TextView | ||||
|         android:id="@+id/custom_text_view" | ||||
|         android:layout_width="wrap_content" | ||||
| 		android:id="@+id/tv_timestamp" | ||||
| 		android:layout_width="0dp" | ||||
| 		android:layout_height="wrap_content" | ||||
| 		android:text="Text" | ||||
| 		android:textColor="#000000" | ||||
|         android:textSize="18sp" /> | ||||
| 		android:textSize="18sp" | ||||
| 		android:layout_weight="1.0"/> | ||||
|  | ||||
| 	<Button | ||||
| 		android:layout_width="wrap_content" | ||||
| 		android:layout_height="wrap_content" | ||||
| 		android:text="复制时间戳" | ||||
| 		android:id="@+id/btn_copytimestamp"/> | ||||
|  | ||||
| </LinearLayout> | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen