This commit is contained in:
ZhanGSKen 2025-05-05 21:37:22 +08:00
parent 466e061aa8
commit e958556073
7 changed files with 185 additions and 121 deletions

View File

@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Mon May 05 07:42:42 GMT 2025
#Mon May 05 13:36:35 GMT 2025
stageCount=0
libraryProject=
baseVersion=15.0
publishVersion=15.0.0
buildCount=63
buildCount=91
baseBetaVersion=15.0.1

View File

@ -5,12 +5,18 @@ import android.view.View;
import android.widget.Switch;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.LogView;
import cc.winboll.studio.timestamp.R;
import cc.winboll.studio.timestamp.utils.AppConfigsUtil;
import cc.winboll.studio.timestamp.utils.TimeStampRemoteViewsUtil;
import com.hjq.toast.ToastUtils;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
LogView mLogView;
Switch mswEnableMainService;
@ -30,6 +36,8 @@ public class MainActivity extends AppCompatActivity {
ToastUtils.show("onCreate");
}
@Override
protected void onResume() {
super.onResume();
@ -38,5 +46,6 @@ public class MainActivity extends AppCompatActivity {
public void onSetMainServiceStatus(View view) {
MainService.setMainServiceStatus(this, mswEnableMainService.isChecked());
}
}

View File

@ -6,18 +6,17 @@ package cc.winboll.studio.timestamp;
* @Describe 主要服务
*/
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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 androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.timestamp.AssistantService;
import cc.winboll.studio.timestamp.MainService;
@ -26,13 +25,16 @@ import cc.winboll.studio.timestamp.receivers.ButtonClickReceiver;
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.util.Timer;
import android.app.NotificationManager;
import java.util.TimerTask;
public class MainService extends Service {
public static String TAG = "MainService";
public static final int MSG_UPDATE_TIMESTAMP = 0;
ButtonClickReceiver mButtonClickReceiver;
NotificationHelper mNotificationHelper;
Notification mNotification;
@ -42,7 +44,7 @@ public class MainService extends Service {
private static boolean _mIsServiceAlive;
public static final String EXTRA_APKFILEPATH = "EXTRA_APKFILEPATH";
final static int MSG_INSTALL_APK = 0;
//MyHandler mMyHandler;
MyHandler mMyHandler;
MyServiceConnection mMyServiceConnection;
MainActivity mInstallCompletedFollowUpActivity;
@ -55,7 +57,7 @@ public class MainService extends Service {
public void onCreate() {
super.onCreate();
// 创建 RemoteViews 对象并使用包含自定义 View 的布局
mRemoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_timestamp);
//mRemoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_timestamp);
// 创建广播接收器实例
@ -69,7 +71,8 @@ public class MainService extends Service {
LogUtils.d(TAG, "onCreate()");
_mIsServiceAlive = false;
//mMyHandler = new MyHandler(MainService.this);
mMyHandler = new MyHandler();
if (mMyServiceConnection == null) {
mMyServiceConnection = new MyServiceConnection();
}
@ -85,16 +88,27 @@ public class MainService extends Service {
_mIsServiceAlive = true;
// 显示前台通知栏
mNotificationHelper = new NotificationHelper(this);
//notification = helper.showForegroundNotification(intent, getString(R.string.app_name), getString(R.string.text_aboutservernotification));
mNotification = mNotificationHelper.showCustomForegroundNotification(new Intent(this, MainActivity.class), mRemoteViews, mRemoteViews);
startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, mNotification);
// mNotificationHelper = new NotificationHelper(this);
// //notification = helper.showForegroundNotification(intent, getString(R.string.app_name), getString(R.string.text_aboutservernotification));
// mNotification = mNotificationHelper.showCustomForegroundNotification(new Intent(this, MainActivity.class), mRemoteViews, mRemoteViews);
// startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, mNotification);
// 唤醒守护进程
wakeupAndBindAssistant();
LogUtils.d(TAG, "running...");
mTimer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
//System.out.println("定时任务执行了");
mMyHandler.sendEmptyMessage(MSG_UPDATE_TIMESTAMP);
}
};
// 延迟2秒后开始执行之后每隔2000毫秒执行一次
mTimer.schedule(task, 2000, 2000);
} else {
@ -180,30 +194,23 @@ public class MainService extends Service {
// //startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, notification);
// }
//
// //
// // 服务事务处理类
// //
// 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);
// }
//
// 服务事务处理类
//
// }
class MyHandler extends Handler {
public void handleMessage(Message message) {
switch (message.what) {
case MSG_UPDATE_TIMESTAMP:
{
TimeStampRemoteViewsUtil.getInstance(MainService.this).showNotification("Hello, World");
LogUtils.d(TAG, "Hello, World");
break;
}
default:
break;
}
super.handleMessage(message);
}
}
}

View File

@ -0,0 +1,81 @@
package cc.winboll.studio.timestamp.utils;
/**
* @Author ZhanGSKen
* @Date 2025/05/05 21:10
* @Describe TimeStampRemoteViewsUtil
*/
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import cc.winboll.studio.timestamp.MainActivity;
import cc.winboll.studio.timestamp.R;
public class TimeStampRemoteViewsUtil {
public static final String TAG = "TimeStampRemoteViewsUtil";
public static final String CHANNEL_ID = "TimeStampChannel";
static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil;
Context mContext;
RemoteViews mRemoteViews;
TimeStampRemoteViewsUtil(Context context) {
mContext = context;
createNotificationChannel();
}
public static synchronized TimeStampRemoteViewsUtil getInstance(Context context) {
if (_TimeStampRemoteViewsUtil == null) {
_TimeStampRemoteViewsUtil = new TimeStampRemoteViewsUtil(context);
}
return _TimeStampRemoteViewsUtil;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "自定义视图通知通道";
String description = "用于展示自定义视图的通知通道";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
public void showNotification(String msg) {
if (mRemoteViews == null) {
// 创建 RemoteViews 对象加载布局
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 的文本
mRemoteViews.setTextViewText(R.id.custom_text_view, msg);
}
}

View File

@ -7,36 +7,22 @@ package cc.winboll.studio.timestamp.views;
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.timestamp.R;
import cc.winboll.studio.timestamp.utils.AppConfigsUtil;
import com.hjq.toast.ToastUtils;
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 TimeStampView extends TextView {
public class TimeStampView extends View {
public static final String TAG = "TimeStampView";
public static final int MSG_UPDATE_TIMESTAMP = 0;
private Paint paint;
//private Paint circlePaint;
//private Paint textPaint;
// Context mContext;
// Timer mTimer;
// TextView mtvTimeStamp;
// MyHandler mMyHandler;
public TimeStampView(Context context) {
super(context);
//initView(context);
@ -55,16 +41,37 @@ public class TimeStampView extends TextView {
init();
}
public TimeStampView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
//initView(context);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
// circlePaint = new Paint();
// circlePaint.setColor(Color.BLUE);
// circlePaint.setStyle(Paint.Style.FILL);
//
// textPaint = new Paint();
// textPaint.setColor(Color.WHITE);
// textPaint.setTextSize(40);
// textPaint.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(50, 50, 30, paint);
//int width = getWidth();
//int height = getHeight();
// int width = 50;
// int height = 50;
// float radius = Math.min(width, height) / 2;
// canvas.drawCircle(width / 2, height / 2, radius, circlePaint);
// String text = "自定义";
// RectF rect = new RectF(0, 0, width, height);
// Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
// int baseline =(int)((rect.bottom + rect.top - fontMetrics.bottom - fontMetrics.top) / 2);
// canvas.drawText(text, width / 2, baseline, textPaint);
}
// void initView(Context context) {
@ -75,17 +82,7 @@ public class TimeStampView extends TextView {
//
// mMyHandler = new MyHandler();
//
// 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, 1000);
// }
// public void updateTimeStamp() {
@ -105,22 +102,5 @@ public class TimeStampView extends TextView {
// }
//
// 服务事务处理类
//
// class MyHandler extends Handler {
//
// public void handleMessage(Message message) {
// switch (message.what) {
// case MSG_UPDATE_TIMESTAMP:
// {
// updateTimeStamp();
// break;
// }
// default:
// break;
// }
// super.handleMessage(message);
// }
// }
}

View File

@ -0,0 +1,15 @@
<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">
<TextView
android:id="@+id/custom_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>

View File

@ -1,28 +0,0 @@
<?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="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"/>
<cc.winboll.studio.timestamp.views.TimeStampView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2"
android:id="@+id/remoteviewstimestampTextView1"/>
<Button
android:id="@+id/btn_copytimestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复制时间戳"/>
</LinearLayout>