添加通知栏时间戳显示
This commit is contained in:
parent
f0a29dc7a9
commit
2e9b6ae263
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#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
|
stageCount=0
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.0
|
baseVersion=15.0
|
||||||
publishVersion=15.0.0
|
publishVersion=15.0.0
|
||||||
buildCount=8
|
buildCount=15
|
||||||
baseBetaVersion=15.0.1
|
baseBetaVersion=15.0.1
|
||||||
|
@ -7,6 +7,7 @@ import androidx.appcompat.widget.Toolbar;
|
|||||||
import cc.winboll.studio.libappbase.LogView;
|
import cc.winboll.studio.libappbase.LogView;
|
||||||
import com.hjq.toast.ToastUtils;
|
import com.hjq.toast.ToastUtils;
|
||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
|
import cc.winboll.studio.timestamp.models.AppConfigs;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
mswEnableMainService = findViewById(R.id.activitymainSwitch1);
|
mswEnableMainService = findViewById(R.id.activitymainSwitch1);
|
||||||
|
mswEnableMainService.setChecked(AppConfigs.getInstance(this).loadAppConfigs().isEnableService());
|
||||||
|
|
||||||
mLogView = findViewById(R.id.logview);
|
mLogView = findViewById(R.id.logview);
|
||||||
|
|
||||||
|
@ -11,23 +11,41 @@ import android.content.ComponentName;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
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.libappbase.LogUtils;
|
||||||
import cc.winboll.studio.timestamp.AssistantService;
|
import cc.winboll.studio.timestamp.AssistantService;
|
||||||
import cc.winboll.studio.timestamp.MainService;
|
import cc.winboll.studio.timestamp.MainService;
|
||||||
import cc.winboll.studio.timestamp.models.AppConfigs;
|
import cc.winboll.studio.timestamp.models.AppConfigs;
|
||||||
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 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 class MainService extends Service {
|
||||||
|
|
||||||
public static String TAG = "MainService";
|
public static String TAG = "MainService";
|
||||||
|
|
||||||
|
public static final int MSG_UPDATE_TIMESTAMP = 0;
|
||||||
|
|
||||||
|
Intent intentMainService;
|
||||||
|
NotificationHelper mNotificationHelper;
|
||||||
Notification notification;
|
Notification notification;
|
||||||
|
RemoteViews mRemoteViews;
|
||||||
|
TextView mtvTimeStamp;
|
||||||
|
Timer mTimer;
|
||||||
private static boolean _mIsServiceAlive;
|
private static boolean _mIsServiceAlive;
|
||||||
public static final String EXTRA_APKFILEPATH = "EXTRA_APKFILEPATH";
|
public static final String EXTRA_APKFILEPATH = "EXTRA_APKFILEPATH";
|
||||||
final static int MSG_INSTALL_APK = 0;
|
final static int MSG_INSTALL_APK = 0;
|
||||||
//Handler mHandler;
|
MyHandler mMyHandler;
|
||||||
MyServiceConnection mMyServiceConnection;
|
MyServiceConnection mMyServiceConnection;
|
||||||
MainActivity mInstallCompletedFollowUpActivity;
|
MainActivity mInstallCompletedFollowUpActivity;
|
||||||
|
|
||||||
@ -39,9 +57,13 @@ public class MainService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
||||||
|
mRemoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_timestamp);
|
||||||
|
intentMainService = new Intent(this, MainActivity.class);
|
||||||
|
|
||||||
LogUtils.d(TAG, "onCreate()");
|
LogUtils.d(TAG, "onCreate()");
|
||||||
_mIsServiceAlive = false;
|
_mIsServiceAlive = false;
|
||||||
//mHandler = new MyHandler(MainService.this);
|
mMyHandler = new MyHandler(MainService.this);
|
||||||
if (mMyServiceConnection == null) {
|
if (mMyServiceConnection == null) {
|
||||||
mMyServiceConnection = new MyServiceConnection();
|
mMyServiceConnection = new MyServiceConnection();
|
||||||
}
|
}
|
||||||
@ -56,17 +78,27 @@ public class MainService extends Service {
|
|||||||
// 设置运行状态
|
// 设置运行状态
|
||||||
_mIsServiceAlive = true;
|
_mIsServiceAlive = true;
|
||||||
|
|
||||||
|
|
||||||
// 显示前台通知栏
|
// 显示前台通知栏
|
||||||
NotificationHelper helper = new NotificationHelper(this);
|
mNotificationHelper = new NotificationHelper(this);
|
||||||
Intent intent = new Intent(this, MainActivity.class);
|
//notification = helper.showForegroundNotification(intent, getString(R.string.app_name), getString(R.string.text_aboutservernotification));
|
||||||
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);
|
startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, notification);
|
||||||
|
|
||||||
// 唤醒守护进程
|
// 唤醒守护进程
|
||||||
wakeupAndBindAssistant();
|
wakeupAndBindAssistant();
|
||||||
|
|
||||||
LogUtils.d(TAG, "running...");
|
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 {
|
} else {
|
||||||
LogUtils.d(TAG, "_mIsServiceAlive is " + Boolean.toString(_mIsServiceAlive));
|
LogUtils.d(TAG, "_mIsServiceAlive is " + Boolean.toString(_mIsServiceAlive));
|
||||||
@ -78,6 +110,9 @@ public class MainService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
if (mTimer != null) {
|
||||||
|
mTimer.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
_mIsServiceAlive = false;
|
_mIsServiceAlive = false;
|
||||||
LogUtils.d(TAG, "onDestroy()");
|
LogUtils.d(TAG, "onDestroy()");
|
||||||
@ -104,8 +139,8 @@ public class MainService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 主进程与守护进程连接时需要用到此类
|
// 主进程与守护进程连接时需要用到此类
|
||||||
//
|
//
|
||||||
private class MyServiceConnection implements ServiceConnection {
|
private class MyServiceConnection implements ServiceConnection {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
@ -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 {
|
class MyHandler extends Handler {
|
||||||
// WeakReference<MainService> weakReference;
|
WeakReference<MainService> weakReference;
|
||||||
// MyHandler(MainService service) {
|
MyHandler(MainService service) {
|
||||||
// weakReference = new WeakReference<MainService>(service);
|
weakReference = new WeakReference<MainService>(service);
|
||||||
// }
|
}
|
||||||
// public void handleMessage(Message message) {
|
public void handleMessage(Message message) {
|
||||||
// MainService theActivity = weakReference.get();
|
MainService theService = weakReference.get();
|
||||||
// switch (message.what) {
|
switch (message.what) {
|
||||||
// case MSG_INSTALL_APK:
|
case MSG_UPDATE_TIMESTAMP:
|
||||||
// {
|
{
|
||||||
// break;
|
if (theService != null) {
|
||||||
// }
|
theService.updateTimeStamp();
|
||||||
// default:
|
}
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
// super.handleMessage(message);
|
default:
|
||||||
// }
|
break;
|
||||||
// }
|
}
|
||||||
|
super.handleMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendUpdateTimeStampMessage() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,28 @@ public class NotificationHelper {
|
|||||||
mNotificationManager.notify(FOREGROUND_NOTIFICATION_ID, notification);
|
mNotificationManager.notify(FOREGROUND_NOTIFICATION_ID, notification);
|
||||||
return notification;
|
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) {
|
public void showTemporaryNotification(Intent intent, String title, String 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>
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user