添加通知栏时间戳显示
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Mon May 05 02:43:50 GMT 2025 | ||||
| #Mon May 05 03:29:39 GMT 2025 | ||||
| stageCount=0 | ||||
| libraryProject= | ||||
| baseVersion=15.0 | ||||
| publishVersion=15.0.0 | ||||
| buildCount=8 | ||||
| buildCount=15 | ||||
| baseBetaVersion=15.0.1 | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import androidx.appcompat.widget.Toolbar; | ||||
| import cc.winboll.studio.libappbase.LogView; | ||||
| import com.hjq.toast.ToastUtils; | ||||
| import android.widget.Switch; | ||||
| import cc.winboll.studio.timestamp.models.AppConfigs; | ||||
|  | ||||
| public class MainActivity extends AppCompatActivity { | ||||
|  | ||||
| @@ -22,6 +23,7 @@ public class MainActivity extends AppCompatActivity { | ||||
| 		setSupportActionBar(toolbar); | ||||
|  | ||||
|         mswEnableMainService = findViewById(R.id.activitymainSwitch1); | ||||
|         mswEnableMainService.setChecked(AppConfigs.getInstance(this).loadAppConfigs().isEnableService()); | ||||
|  | ||||
|         mLogView = findViewById(R.id.logview); | ||||
|  | ||||
|   | ||||
| @@ -11,23 +11,41 @@ import android.content.ComponentName; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.content.ServiceConnection; | ||||
| import android.os.Handler; | ||||
| import android.os.IBinder; | ||||
| import android.os.Message; | ||||
| import android.widget.RemoteViews; | ||||
| import android.widget.TextView; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import cc.winboll.studio.timestamp.AssistantService; | ||||
| import cc.winboll.studio.timestamp.MainService; | ||||
| import cc.winboll.studio.timestamp.models.AppConfigs; | ||||
| import cc.winboll.studio.timestamp.utils.NotificationHelper; | ||||
| import cc.winboll.studio.timestamp.utils.ServiceUtil; | ||||
| import java.lang.ref.WeakReference; | ||||
| 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; | ||||
|  | ||||
| public class MainService extends Service { | ||||
|  | ||||
|     public static String TAG = "MainService"; | ||||
|  | ||||
|     public static final int MSG_UPDATE_TIMESTAMP = 0; | ||||
|  | ||||
|     Intent intentMainService; | ||||
|     NotificationHelper mNotificationHelper; | ||||
|     Notification notification; | ||||
|     RemoteViews mRemoteViews; | ||||
|     TextView mtvTimeStamp; | ||||
|     Timer mTimer; | ||||
|     private static boolean _mIsServiceAlive; | ||||
|     public static final String EXTRA_APKFILEPATH = "EXTRA_APKFILEPATH"; | ||||
|     final static int MSG_INSTALL_APK = 0; | ||||
|     //Handler mHandler; | ||||
|     MyHandler mMyHandler; | ||||
|     MyServiceConnection mMyServiceConnection; | ||||
|     MainActivity mInstallCompletedFollowUpActivity; | ||||
|  | ||||
| @@ -39,9 +57,13 @@ public class MainService extends Service { | ||||
|     @Override | ||||
|     public void onCreate() { | ||||
|         super.onCreate(); | ||||
|  | ||||
|         mRemoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_timestamp); | ||||
|         intentMainService = new Intent(this, MainActivity.class); | ||||
|          | ||||
|         LogUtils.d(TAG, "onCreate()"); | ||||
|         _mIsServiceAlive = false; | ||||
|         //mHandler = new MyHandler(MainService.this); | ||||
|         mMyHandler = new MyHandler(MainService.this); | ||||
|         if (mMyServiceConnection == null) { | ||||
|             mMyServiceConnection = new MyServiceConnection(); | ||||
|         } | ||||
| @@ -56,17 +78,27 @@ public class MainService extends Service { | ||||
|                 // 设置运行状态 | ||||
|                 _mIsServiceAlive = true; | ||||
|  | ||||
|  | ||||
|                 // 显示前台通知栏 | ||||
|                 NotificationHelper helper = new NotificationHelper(this); | ||||
|                 Intent intent = new Intent(this, MainActivity.class); | ||||
|                 notification = helper.showForegroundNotification(intent, getString(R.string.app_name), getString(R.string.text_aboutservernotification)); | ||||
|                 mNotificationHelper = new NotificationHelper(this); | ||||
|                 //notification = helper.showForegroundNotification(intent, getString(R.string.app_name), getString(R.string.text_aboutservernotification)); | ||||
|                 notification = mNotificationHelper.showCustomForegroundNotification(intentMainService, mRemoteViews, mRemoteViews); | ||||
|                 startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, notification); | ||||
|  | ||||
|                 // 唤醒守护进程 | ||||
|                 wakeupAndBindAssistant(); | ||||
|  | ||||
|                 LogUtils.d(TAG, "running..."); | ||||
|                 mTimer = new Timer(); | ||||
|                 TimerTask task = new TimerTask() { | ||||
|                     @Override | ||||
|                     public void run() { | ||||
|                         //System.out.println("定时任务执行了"); | ||||
|                         mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP); | ||||
|                     } | ||||
|                 }; | ||||
|                 // 延迟1秒后开始执行,之后每隔2秒执行一次 | ||||
|                 mTimer.schedule(task, 1000, 2000); | ||||
|  | ||||
|  | ||||
|             } else { | ||||
|                 LogUtils.d(TAG, "_mIsServiceAlive is " + Boolean.toString(_mIsServiceAlive)); | ||||
| @@ -78,6 +110,9 @@ public class MainService extends Service { | ||||
|     @Override | ||||
|     public void onDestroy() { | ||||
|         super.onDestroy(); | ||||
|         if (mTimer != null) { | ||||
|             mTimer.cancel(); | ||||
|         } | ||||
|  | ||||
|         _mIsServiceAlive = false; | ||||
|         LogUtils.d(TAG, "onDestroy()"); | ||||
| @@ -133,25 +168,46 @@ public class MainService extends Service { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     void updateTimeStamp() { | ||||
|         long currentMillis = System.currentTimeMillis(); | ||||
|         Instant instant = Instant.ofEpochMilli(currentMillis); | ||||
|         LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); | ||||
|         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||||
|         String formattedDateTime = ldt.format(formatter); | ||||
|         //System.out.println(formattedDateTime); | ||||
|         mRemoteViews.setTextViewText(R.id.tv_timestamp, formattedDateTime); | ||||
|         notification = mNotificationHelper.showCustomForegroundNotification(intentMainService, mRemoteViews, mRemoteViews); | ||||
|         //startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, notification); | ||||
|     } | ||||
|  | ||||
|     //  | ||||
|     // 服务事务处理类 | ||||
|     // | ||||
| //    static class MyHandler extends Handler { | ||||
| //        WeakReference<MainService> weakReference;   | ||||
| //        MyHandler(MainService service) {   | ||||
| //            weakReference = new WeakReference<MainService>(service);   | ||||
| //        } | ||||
| //        public void handleMessage(Message message) { | ||||
| //            MainService theActivity = weakReference.get(); | ||||
| //            switch (message.what) { | ||||
| //                case MSG_INSTALL_APK: | ||||
| //                    { | ||||
| //                        break; | ||||
| //                    } | ||||
| //                default: | ||||
| //                    break; | ||||
| //            } | ||||
| //            super.handleMessage(message); | ||||
| //        } | ||||
| //    } | ||||
|     class MyHandler extends Handler { | ||||
|         WeakReference<MainService> weakReference;   | ||||
|         MyHandler(MainService service) {   | ||||
|             weakReference = new WeakReference<MainService>(service);   | ||||
|         } | ||||
|         public void handleMessage(Message message) { | ||||
|             MainService theService = weakReference.get(); | ||||
|             switch (message.what) { | ||||
|                 case MSG_UPDATE_TIMESTAMP: | ||||
|                     { | ||||
|                         if (theService != null) { | ||||
|                             theService.updateTimeStamp(); | ||||
|                         } | ||||
|                         break; | ||||
|                     } | ||||
|                 default: | ||||
|                     break; | ||||
|             } | ||||
|             super.handleMessage(message); | ||||
|         } | ||||
|  | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public void sendUpdateTimeStampMessage() { | ||||
|  | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -114,6 +114,28 @@ public class NotificationHelper { | ||||
|         return notification; | ||||
|     } | ||||
|      | ||||
|  | ||||
|     // 显示常驻通知(通常用于前台服务) | ||||
|     public Notification showCustomForegroundNotification(Intent intent, RemoteViews contentView, RemoteViews bigContentView) { | ||||
|         PendingIntent pendingIntent = createPendingIntent(intent); | ||||
|          | ||||
|          | ||||
|         Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMPORARY) | ||||
|             .setSmallIcon(R.drawable.ic_launcher) | ||||
|             .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher)) | ||||
|             //.setContentTitle(title) | ||||
|             .setContentIntent(pendingIntent) | ||||
|             .setContent(contentView) | ||||
|             .setCustomBigContentView(bigContentView) | ||||
|             .setPriority(NotificationCompat.PRIORITY_HIGH) | ||||
|             .setAutoCancel(true) | ||||
|             .setOngoing(true) | ||||
|             .build(); | ||||
|  | ||||
|         mNotificationManager.notify(FOREGROUND_NOTIFICATION_ID, notification); | ||||
|         return notification; | ||||
|     } | ||||
|  | ||||
|     // 显示临时通知(自动消失) | ||||
|     public void showTemporaryNotification(Intent intent, String title, String content) { | ||||
|         showTemporaryNotification(intent, TEMPORARY_NOTIFICATION_ID, title, content); | ||||
|   | ||||
| @@ -1,90 +0,0 @@ | ||||
| package cc.winboll.studio.timestamp.utils; | ||||
|  | ||||
| /** | ||||
|  * @Author ZhanGSKen | ||||
|  * @Date 2025/05/05 09:45 | ||||
|  * @Describe 通知栏工具 | ||||
|  */ | ||||
| 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.BitmapFactory; | ||||
| import android.graphics.Color; | ||||
| import android.media.RingtoneManager; | ||||
| import android.os.Build; | ||||
| import android.util.Log; | ||||
| import android.widget.RemoteViews; | ||||
| import cc.winboll.studio.timestamp.MainService; | ||||
| import cc.winboll.studio.timestamp.MainActivity; | ||||
| import cc.winboll.studio.timestamp.R; | ||||
|  | ||||
| public class NotificationUtil { | ||||
|      | ||||
|     public static final String TAG = "NotificationUtil"; | ||||
|      | ||||
|      | ||||
|      | ||||
|     static final String szServiceChannelID = "0"; | ||||
|     static int mNumSendForegroundNotification = 10000; | ||||
|  | ||||
|     public NotificationManager createServiceNotificationChannel(Context context) { | ||||
|         //创建通知渠道ID | ||||
|         String channelId = szServiceChannelID; | ||||
|         //创建通知渠道名称 | ||||
|         String channelName = "Service Message"; | ||||
|         //创建通知渠道重要性 | ||||
|         int importance = NotificationManager.IMPORTANCE_MIN; | ||||
|         NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); | ||||
|         channel.setSound(null, null); | ||||
|         NotificationManager notificationManager = (NotificationManager) context.getSystemService( | ||||
|             Context.NOTIFICATION_SERVICE); | ||||
|         notificationManager.createNotificationChannel(channel); | ||||
|         return notificationManager; | ||||
|     } | ||||
|  | ||||
|     // 创建通知 | ||||
|     // | ||||
|     public void sendForegroundNotification(MainService service) { | ||||
|         //创建Notification,传入Context和channelId | ||||
|         Intent intent = new Intent();//这个intent会传给目标,可以使用getIntent来获取 | ||||
|         intent.setClass(service, MainActivity.class); | ||||
|  | ||||
|         //这里放一个count用来区分每一个通知 | ||||
|         //intent.putExtra("intent", "intent--->" + count);//这里设置一个数据,带过去 | ||||
|  | ||||
|         //参数1:context 上下文对象 | ||||
|         //参数2:发送者私有的请求码(Private request code for the sender) | ||||
|         //参数3:intent 意图对象 | ||||
|         //参数4:必须为FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT,中的一个 | ||||
|         PendingIntent mForegroundPendingIntent = PendingIntent.getActivity(service, mNumSendForegroundNotification, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT); | ||||
|  | ||||
|         Notification mForegroundNotification = new Notification.Builder(service, szServiceChannelID) | ||||
|             .setAutoCancel(true) | ||||
|             .setContentTitle(service.getString(R.string.app_name)) | ||||
|             .setContentText(service.TAG + " is started.") | ||||
|             .setWhen(System.currentTimeMillis()) | ||||
|             .setSmallIcon(R.drawable.ic_launcher) | ||||
|             //设置红色 | ||||
|             .setColor(Color.parseColor("#F00606")) | ||||
|             .setLargeIcon(BitmapFactory.decodeResource(service.getResources(), R.drawable.ic_launcher)) | ||||
|             .setContentIntent(mForegroundPendingIntent) | ||||
|             .build(); | ||||
|  | ||||
|  | ||||
|         /*RemoteViews mrvForegroundNotificationView = new RemoteViews(service.getPackageName(), R.layout.remoteview); | ||||
|          mrvForegroundNotificationView.setTextViewText(R.id.remoteviewTextView1, notificationMessage.getTitle()); | ||||
|          mrvForegroundNotificationView.setTextViewText(R.id.remoteviewTextView2, notificationMessage.getContent()); | ||||
|          mrvForegroundNotificationView.setImageViewResource(R.id.remoteviewImageView1, R.drawable.ic_launcher); | ||||
|          mForegroundNotification.contentView = mrvForegroundNotificationView; | ||||
|          mForegroundNotification.bigContentView = mrvForegroundNotificationView; | ||||
|          */ | ||||
|  | ||||
|         service.startForeground(mNumSendForegroundNotification, mForegroundNotification); | ||||
|  | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
							
								
								
									
										16
									
								
								timestamp/src/main/res/layout/remoteviews_timestamp.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								timestamp/src/main/res/layout/remoteviews_timestamp.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <LinearLayout | ||||
| 	xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| 	xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
| 	android:orientation="vertical" | ||||
| 	android:layout_width="match_parent" | ||||
| 	android:layout_height="match_parent"> | ||||
|  | ||||
| 	<TextView | ||||
| 		android:layout_width="wrap_content" | ||||
| 		android:layout_height="wrap_content" | ||||
| 		android:text="Text" | ||||
| 		android:id="@+id/tv_timestamp"/> | ||||
|  | ||||
| </LinearLayout> | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen