Commit 1e6d3e54 authored by maxiaoliang's avatar maxiaoliang

定时器

parent c7c3c2f5
package com.base.datarecovery.fcm;
import android.Manifest;
import android.app.Notification;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationManagerCompat;
import com.base.datarecovery.MyApplication;
import com.base.datarecovery.help.BaseApplication;
import com.base.datarecovery.service.StayNotificationService;
import java.util.Timer;
import java.util.TimerTask;
public class NotificationTimerManager {
private static NotificationTimerManager instance;
private Timer timer;
private boolean isTimerRunning;
private TimerTask timerTask;
private NotificationTimerManager() {
isTimerRunning = false;
}
public static synchronized NotificationTimerManager getInstance() {
if (instance == null) {
instance = new NotificationTimerManager();
}
return instance;
}
public void startTimer(long initialDelay, long intervalPeriod) {
synchronized (this) { // 使用 synchronized 确保线程安全
if (!isTimerRunning) {
cancelExistingTimer(); // 取消现有的定时器和任务
// 创建新的 TimerTask 实例并调度
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
Notification notification = StayNotificationService.Companion.createPermanentNotification(MyApplication.context);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MyApplication.context);
if (ActivityCompat.checkSelfPermission(MyApplication.context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
return;
}
notificationManager.notify(1, notification);
}
};
timer.schedule(timerTask, initialDelay, intervalPeriod);
isTimerRunning = true;
}
}
}
public void stopTimer() {
synchronized (this) { // 使用 synchronized 确保线程安全
if (isTimerRunning) {
cancelExistingTimer();
isTimerRunning = false;
}
}
}
private void cancelExistingTimer() {
if (timerTask != null) {
timerTask.cancel();
}
if (timer != null) {
timer.cancel();
timer.purge();
}
}
public boolean isTimerRunning() {
return isTimerRunning;
}
}
\ No newline at end of file
...@@ -47,38 +47,7 @@ class StayNotificationService : Service() { ...@@ -47,38 +47,7 @@ class StayNotificationService : Service() {
startService(intent) startService(intent)
} }
} }
} fun createPermanentNotification(context: Context): Notification {
@SuppressLint("ForegroundServiceType")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (!isRunning) {
val notification = createPermanentNotification(applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else {
startForeground(1, notification)
}
isRunning = true
}
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onDestroy() {
isRunning = false
super.onDestroy()
// 取消订阅
}
private fun createPermanentNotification(context: Context): Notification {
val isOngoing = true //是否持续(为不消失的常驻通知) val isOngoing = true //是否持续(为不消失的常驻通知)
val channelName = "File Recovery Foreground Service Channel" val channelName = "File Recovery Foreground Service Channel"
...@@ -140,14 +109,12 @@ class StayNotificationService : Service() { ...@@ -140,14 +109,12 @@ class StayNotificationService : Service() {
val builder = NotificationCompat.Builder(context, channelId) val builder = NotificationCompat.Builder(context, channelId)
val smallIcon = IconCompat.createFromIcon( // val smallIcon = IconCompat.createFromIcon(
context, Icon.createWithResource( // context, Icon.createWithResource(
this, R.drawable.icon_100 // this,
) // )
) // )
smallIcon?.let { builder.setSmallIcon(R.drawable.icon_100)
builder.setSmallIcon(smallIcon) //设置状态栏内的小图标
}
builder.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.logo)) builder.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.logo))
builder.setContentTitle(context.resources.getString(R.string.app_name)) builder.setContentTitle(context.resources.getString(R.string.app_name))
builder.setContentIntent(pendingIntent) //设置PendingIntent builder.setContentIntent(pendingIntent) //设置PendingIntent
...@@ -169,4 +136,36 @@ class StayNotificationService : Service() { ...@@ -169,4 +136,36 @@ class StayNotificationService : Service() {
} }
return builder.build() return builder.build()
} }
}
@SuppressLint("ForegroundServiceType")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (!isRunning) {
val notification = createPermanentNotification(applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else {
startForeground(1, notification)
}
isRunning = true
}
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onDestroy() {
isRunning = false
super.onDestroy()
// 取消订阅
}
} }
\ No newline at end of file
package com.base.datarecovery.utils package com.base.datarecovery.utils
import android.os.Build
import android.text.TextUtils import android.text.TextUtils
import androidx.annotation.RequiresApi
import com.android.installreferrer.api.InstallReferrerClient import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.BuildConfig import com.base.datarecovery.BuildConfig
import com.base.datarecovery.ads.AdmobMaxHelper import com.base.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.fcm.NotificationTimerManager
import com.base.datarecovery.fcm.RecoveryTimerManager import com.base.datarecovery.fcm.RecoveryTimerManager
import com.base.datarecovery.help.BaseApplication import com.base.datarecovery.help.BaseApplication
import org.json.JSONObject import org.json.JSONObject
...@@ -79,11 +82,24 @@ object InstallHelps { ...@@ -79,11 +82,24 @@ object InstallHelps {
}) })
} }
@RequiresApi(Build.VERSION_CODES.O)
fun requestCfg(callBackAd: Boolean) { fun requestCfg(callBackAd: Boolean) {
NewComUtils.requestCfg { NewComUtils.requestCfg {
if (callBackAd) { if (callBackAd) {
AdmobMaxHelper.initAdmobMaxAd() AdmobMaxHelper.initAdmobMaxAd()
} }
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE){
val notificationSwitch=AppPreferences.getInstance().getString("isjunkPlayAd", "0").toInt()
if(notificationSwitch==1){
val notificationTimeInterval=AppPreferences.getInstance().getString("notificationTimeInterval", "0").toInt()
NotificationTimerManager.getInstance()
.startTimer(
( notificationTimeInterval * 1000).toLong(),
( notificationTimeInterval * 1000).toLong()
)
}
}
val timerStatus: Int = val timerStatus: Int =
AppPreferences.getInstance().getString("timerS", "1") AppPreferences.getInstance().getString("timerS", "1")
.toIntOrNull() ?: 1 .toIntOrNull() ?: 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment