重构常驻通知栏
This commit is contained in:
		| @@ -1,8 +1,8 @@ | |||||||
| #Created by .winboll/winboll_app_build.gradle | #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 | stageCount=0 | ||||||
| libraryProject= | libraryProject= | ||||||
| baseVersion=15.0 | baseVersion=15.0 | ||||||
| publishVersion=15.0.0 | publishVersion=15.0.0 | ||||||
| buildCount=91 | buildCount=101 | ||||||
| baseBetaVersion=15.0.1 | 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.NotificationHelper; | ||||||
| import cc.winboll.studio.timestamp.utils.ServiceUtil; | import cc.winboll.studio.timestamp.utils.ServiceUtil; | ||||||
| import cc.winboll.studio.timestamp.utils.TimeStampRemoteViewsUtil; | 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.Timer; | ||||||
| import java.util.TimerTask; | import java.util.TimerTask; | ||||||
|  |  | ||||||
| @@ -106,8 +110,8 @@ public class MainService extends Service { | |||||||
|                         mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP); |                         mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP); | ||||||
|                     } |                     } | ||||||
|                 }; |                 }; | ||||||
|                 // 延迟2秒后开始执行,之后每隔2000毫秒执行一次 |                 // 延迟1秒后开始执行,之后每隔100毫秒执行一次 | ||||||
|                 mTimer.schedule(task, 2000, 2000); |                 mTimer.schedule(task, 1000, 100); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -203,8 +207,15 @@ public class MainService extends Service { | |||||||
|             switch (message.what) { |             switch (message.what) { | ||||||
|                 case MSG_UPDATE_TIMESTAMP: |                 case MSG_UPDATE_TIMESTAMP: | ||||||
|                     { |                     { | ||||||
|                         TimeStampRemoteViewsUtil.getInstance(MainService.this).showNotification("Hello, World"); |                         long currentMillis = System.currentTimeMillis(); | ||||||
|                         LogUtils.d(TAG, "Hello, World"); |                         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; |                         break; | ||||||
|                     } |                     } | ||||||
|                 default: |                 default: | ||||||
|   | |||||||
| @@ -15,6 +15,8 @@ import android.widget.RemoteViews; | |||||||
| import androidx.core.app.NotificationCompat; | 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.views.TimeStampView; | ||||||
|  | import android.widget.TextView; | ||||||
|  |  | ||||||
| public class TimeStampRemoteViewsUtil { | public class TimeStampRemoteViewsUtil { | ||||||
|  |  | ||||||
| @@ -25,6 +27,7 @@ public class TimeStampRemoteViewsUtil { | |||||||
|     static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil; |     static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil; | ||||||
|     Context mContext; |     Context mContext; | ||||||
|     RemoteViews mRemoteViews; |     RemoteViews mRemoteViews; | ||||||
|  |     TextView mtvMessage; | ||||||
|  |  | ||||||
|     TimeStampRemoteViewsUtil(Context context) { |     TimeStampRemoteViewsUtil(Context context) { | ||||||
|         mContext = context; |         mContext = context; | ||||||
| @@ -54,28 +57,29 @@ public class TimeStampRemoteViewsUtil { | |||||||
|         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 的文本颜色 |              | ||||||
|             mRemoteViews.setTextColor(R.id.custom_text_view, mContext.getResources().getColor(R.color.colorAccent, null)); |  | ||||||
|             // 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题) |  | ||||||
|  |  | ||||||
|             // 创建点击通知后的意图 |  | ||||||
|             Intent intent = new Intent(mContext, MainActivity.class); |  | ||||||
|             PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |  | ||||||
|  |  | ||||||
|             // 设置通知的点击事件 |  | ||||||
|             mRemoteViews.setOnClickPendingIntent(R.id.custom_text_view, pendingIntent); |  | ||||||
|  |  | ||||||
|             // 构建通知 |  | ||||||
|             NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID) |  | ||||||
|                 .setSmallIcon(android.R.drawable.ic_dialog_info) |  | ||||||
|                 .setContent(mRemoteViews) |  | ||||||
|                 .setAutoCancel(true); |  | ||||||
|  |  | ||||||
|             // 显示通知 |  | ||||||
|             NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class); |  | ||||||
|             notificationManager.notify(1, builder.build()); |  | ||||||
|         } |         } | ||||||
|         // 自定义 TextView 的文本 |         // 自定义 TextView 的文本 | ||||||
|         mRemoteViews.setTextViewText(R.id.custom_text_view, msg); |         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 pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 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) | ||||||
|  |             .setAutoCancel(true); | ||||||
|  |  | ||||||
|  |         // 显示通知 | ||||||
|  |         NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class); | ||||||
|  |         notificationManager.notify(1, builder.build()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -87,14 +87,7 @@ public class TimeStampView extends View { | |||||||
|  |  | ||||||
| //    public void updateTimeStamp() { | //    public void updateTimeStamp() { | ||||||
| //        try { | //        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) { | //        } catch (Exception e) { | ||||||
| //            LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | //            LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||||
| //            ToastUtils.show(e); | //            ToastUtils.show(e); | ||||||
|   | |||||||
| @@ -1,15 +1,26 @@ | |||||||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | <?xml version="1.0" encoding="utf-8"?> | ||||||
|     android:layout_width="match_parent" | <LinearLayout | ||||||
|     android:layout_height="match_parent" | 	xmlns:android="http://schemas.android.com/apk/res/android" | ||||||
|     android:orientation="vertical" | 	android:layout_width="match_parent" | ||||||
|     android:padding="16dp"> | 	android:layout_height="match_parent" | ||||||
|  | 	android:orientation="horizontal" | ||||||
|  | 	android:padding="16dp" | ||||||
|  | 	android:gravity="center_vertical"> | ||||||
|  |  | ||||||
|     <TextView | 	<TextView | ||||||
|         android:id="@+id/custom_text_view" | 		android:id="@+id/tv_timestamp" | ||||||
|         android:layout_width="wrap_content" | 		android:layout_width="0dp" | ||||||
|         android:layout_height="wrap_content" | 		android:layout_height="wrap_content" | ||||||
|         android:text="Text" | 		android:text="Text" | ||||||
|         android:textColor="#000000" | 		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> | </LinearLayout> | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen