优化常驻通知电量消耗
This commit is contained in:
		@@ -1,8 +1,8 @@
 | 
			
		||||
#Created by .winboll/winboll_app_build.gradle
 | 
			
		||||
#Tue May 06 09:02:43 GMT 2025
 | 
			
		||||
#Tue May 06 09:59:03 GMT 2025
 | 
			
		||||
stageCount=3
 | 
			
		||||
libraryProject=
 | 
			
		||||
baseVersion=15.0
 | 
			
		||||
publishVersion=15.0.2
 | 
			
		||||
buildCount=2
 | 
			
		||||
buildCount=14
 | 
			
		||||
baseBetaVersion=15.0.3
 | 
			
		||||
 
 | 
			
		||||
@@ -5,9 +5,13 @@
 | 
			
		||||
 | 
			
		||||
    <!-- 运行前台服务 -->
 | 
			
		||||
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
 | 
			
		||||
    <uses-permission android:name="android.permission.READ_CLIPBOARD" />
 | 
			
		||||
    <uses-permission android:name="android.permission.WRITE_CLIPBOARD" />
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    <!-- READ_CLIPBOARD -->
 | 
			
		||||
    <uses-permission android:name="android.permission.READ_CLIPBOARD"/>
 | 
			
		||||
 | 
			
		||||
    <!-- WRITE_CLIPBOARD -->
 | 
			
		||||
    <uses-permission android:name="android.permission.WRITE_CLIPBOARD"/>
 | 
			
		||||
 | 
			
		||||
    <application
 | 
			
		||||
        android:allowBackup="true"
 | 
			
		||||
        android:icon="@mipmap/ic_launcher"
 | 
			
		||||
 
 | 
			
		||||
@@ -31,8 +31,6 @@ 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 {
 | 
			
		||||
 | 
			
		||||
@@ -45,11 +43,11 @@ public class MainService extends Service {
 | 
			
		||||
    Notification mNotification;
 | 
			
		||||
    RemoteViews mRemoteViews;
 | 
			
		||||
    TextView mtvTimeStamp;
 | 
			
		||||
    Timer mTimer;
 | 
			
		||||
    //Timer mTimer;
 | 
			
		||||
    private static boolean _mIsServiceAlive;
 | 
			
		||||
    public static final String EXTRA_APKFILEPATH = "EXTRA_APKFILEPATH";
 | 
			
		||||
    final static int MSG_INSTALL_APK = 0;
 | 
			
		||||
    MyHandler mMyHandler;
 | 
			
		||||
    static MyHandler _MyHandler;
 | 
			
		||||
    MyServiceConnection mMyServiceConnection;
 | 
			
		||||
    MainActivity mInstallCompletedFollowUpActivity;
 | 
			
		||||
 | 
			
		||||
@@ -77,7 +75,7 @@ public class MainService extends Service {
 | 
			
		||||
        LogUtils.d(TAG, "onCreate()");
 | 
			
		||||
        _mIsServiceAlive = false;
 | 
			
		||||
 | 
			
		||||
        mMyHandler = new MyHandler();
 | 
			
		||||
        _MyHandler = new MyHandler();
 | 
			
		||||
        if (mMyServiceConnection == null) {
 | 
			
		||||
            mMyServiceConnection = new MyServiceConnection();
 | 
			
		||||
        }
 | 
			
		||||
@@ -102,17 +100,18 @@ public class MainService extends Service {
 | 
			
		||||
                wakeupAndBindAssistant();
 | 
			
		||||
 | 
			
		||||
                LogUtils.d(TAG, "running...");
 | 
			
		||||
                _MyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP);
 | 
			
		||||
 | 
			
		||||
                mTimer = new Timer();
 | 
			
		||||
                TimerTask task = new TimerTask() {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void run() {
 | 
			
		||||
                        //System.out.println("定时任务执行了");
 | 
			
		||||
                        mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP);
 | 
			
		||||
                    }
 | 
			
		||||
                };
 | 
			
		||||
                // 延迟1秒后开始执行,之后每隔100毫秒执行一次
 | 
			
		||||
                mTimer.schedule(task, 1000, 100);
 | 
			
		||||
//                mTimer = new Timer();
 | 
			
		||||
//                TimerTask task = new TimerTask() {
 | 
			
		||||
//                    @Override
 | 
			
		||||
//                    public void run() {
 | 
			
		||||
//                        //System.out.println("定时任务执行了");
 | 
			
		||||
//                        mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP);
 | 
			
		||||
//                    }
 | 
			
		||||
//                };
 | 
			
		||||
//                // 延迟1秒后开始执行,之后每隔100毫秒执行一次
 | 
			
		||||
//                mTimer.schedule(task, 1000, 100);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -126,14 +125,17 @@ public class MainService extends Service {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onDestroy() {
 | 
			
		||||
        super.onDestroy();
 | 
			
		||||
        if (mTimer != null) {
 | 
			
		||||
            mTimer.cancel();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//        if (mTimer != null) {
 | 
			
		||||
//            mTimer.cancel();
 | 
			
		||||
//        }
 | 
			
		||||
 | 
			
		||||
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
 | 
			
		||||
        notificationManager.cancelAll();
 | 
			
		||||
 | 
			
		||||
        _mIsServiceAlive = false;
 | 
			
		||||
        _MyHandler = null;
 | 
			
		||||
        LogUtils.d(TAG, "onDestroy()");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -217,9 +219,9 @@ public class MainService extends Service {
 | 
			
		||||
                        String szTimeStampFormatString = AppConfigsUtil.getInstance(MainService.this).getAppConfigsModel().getTimeStampFormatString();
 | 
			
		||||
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(szTimeStampFormatString);
 | 
			
		||||
                        String formattedDateTime = ldt.format(formatter);
 | 
			
		||||
                        TimeStampRemoteViewsUtil.getInstance(MainService.this).showNotification(formattedDateTime);
 | 
			
		||||
                        TimeStampRemoteViewsUtil.getInstance(MainService.this).showNotification(formattedDateTime + " 已复制");
 | 
			
		||||
 | 
			
		||||
                        //LogUtils.d(TAG, "Hello, World");
 | 
			
		||||
                        LogUtils.d(TAG, "Hello, World! " + formattedDateTime);
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                default:
 | 
			
		||||
@@ -228,4 +230,10 @@ public class MainService extends Service {
 | 
			
		||||
            super.handleMessage(message);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void updateCopiedTimeStamp() {
 | 
			
		||||
        if (_MyHandler != null) {
 | 
			
		||||
            _MyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ import android.content.Context;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogUtils;
 | 
			
		||||
import cc.winboll.studio.timestamp.MainService;
 | 
			
		||||
import cc.winboll.studio.timestamp.utils.AppConfigsUtil;
 | 
			
		||||
import cc.winboll.studio.timestamp.utils.ClipboardUtil;
 | 
			
		||||
import java.time.Instant;
 | 
			
		||||
@@ -39,6 +40,7 @@ public class ButtonClickReceiver extends BroadcastReceiver {
 | 
			
		||||
 | 
			
		||||
            // 比如显示一个Toast
 | 
			
		||||
            Toast.makeText(context, formattedDateTime + " 已复制", Toast.LENGTH_SHORT).show();
 | 
			
		||||
            MainService.updateCopiedTimeStamp();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -102,31 +102,30 @@ 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.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null));
 | 
			
		||||
            // 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题)
 | 
			
		||||
 | 
			
		||||
            // 创建点击通知后的意图
 | 
			
		||||
            mIntentMain = new Intent(mContext, MainActivity.class);
 | 
			
		||||
            PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, mIntentMain, 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);
 | 
			
		||||
        }
 | 
			
		||||
        // 自定义 TextView 的文本
 | 
			
		||||
        mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
 | 
			
		||||
        // 自定义 TextView 的文本颜色
 | 
			
		||||
        mRemoteViews.setTextColor(R.id.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null));
 | 
			
		||||
        // 这里虽然不能直接设置字体大小,但可以通过反射等方式尝试(不推荐,且有兼容性问题)
 | 
			
		||||
 | 
			
		||||
        // 创建点击通知后的意图
 | 
			
		||||
        mIntentMain = new Intent(mContext, MainActivity.class);
 | 
			
		||||
        PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, mIntentMain, 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);
 | 
			
		||||
 | 
			
		||||
        mNotificationHelper.showCustomForegroundNotification(mIntentMain, mRemoteViews, mRemoteViews);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
<resources>
 | 
			
		||||
    <string name="app_name">TimeStamp</string>
 | 
			
		||||
    <string name="text_aboutservernotification">This is the prompt window when the SMS service runs, which you can set to hide this class notification in the notification message settings.</string>
 | 
			
		||||
    
 | 
			
		||||
    <string name="accessibility_service_description">Accessibility service description.</string>
 | 
			
		||||
</resources>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user