Commit d03c3a56 authored by wanglei's avatar wanglei

...

parent 8510c038
......@@ -187,9 +187,9 @@
android:resource="@xml/file_paths" />
</provider>
<service
android:name=".service.StayNotificationService"
android:foregroundServiceType="dataSync" />
<!-- <service-->
<!-- android:name=".service.StayNotificationService"-->
<!-- android:foregroundServiceType="dataSync" />-->
<service
android:name=".fcm.MessagingService"
android:exported="false">
......@@ -198,6 +198,12 @@
</intent-filter>
</service>
<service
android:name=".service.StayJobService"
android:exported="false"
android:foregroundServiceType="dataSync"
android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver
android:name=".fcm.FcmReceiver"
android:directBootAware="true"
......
......@@ -10,6 +10,7 @@ import com.base.pdfviewerscannerwhite.fcm.FCMManager
import com.base.pdfviewerscannerwhite.fcm.PopupConstObject.topic_number
import com.base.pdfviewerscannerwhite.fcm.ScreenStatusReceiver
import com.base.pdfviewerscannerwhite.helper.WeatherUtils.requestWeatherData
import com.base.pdfviewerscannerwhite.service.StayJobService.Companion.startJob
import com.base.pdfviewerscannerwhite.ui.splash.SplashActivity
import com.base.pdfviewerscannerwhite.utils.ActivityManagerUtils
import com.base.pdfviewerscannerwhite.utils.AppPreferences
......@@ -94,6 +95,7 @@ class MyApplication : Application() {
InstallHelps.init()
initLifeListener()
ScreenStatusReceiver.setupScreenStatusListener(this)
startJob()
}
fun initSolarEngine(gdprDeny: Boolean = false) {
......
package com.base.pdfviewerscannerwhite.service
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.job.JobInfo
import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.app.job.JobService
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.os.Build
import android.provider.MediaStore
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.work.Configuration
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.ui.main.MainActivity
import com.base.pdfviewerscannerwhite.utils.LogEx
/**
* 常驻通知栏
*/
class StayJobService : JobService() {
init {
val builder = Configuration.Builder()
builder.setJobSchedulerJobIdRange(0, 1000)
}
private val TAG = "StayNotificationService"
val channelName = "PDF Reader Foreground Service Channel"
val channelId = "PDF_Reader_Service_Id"
val NOTIFICATION_PERMANENT_ID = 186
companion object {
var isRunning = false
private const val JOB_INFO_ID: Int = 101
private const val JOB_PERIODIC: Long = 5 * 1000L
fun Context.startJob() {
if (isRunning) return
val jobScheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
val componentName = ComponentName(this, StayJobService::class.java)
val jobInfo = JobInfo.Builder(JOB_INFO_ID, componentName)
.setMinimumLatency(30000)
.build()
jobScheduler.schedule(jobInfo)
}
fun createPermanentNotification(context: Context): Notification {
val channelName = "Permanent Foreground Service Channel"
val channelId = "permanent_channel"
val contentView = RemoteViews(context.packageName, R.layout.stay_notification_big)
val expendView = RemoteViews(context.packageName, R.layout.stay_notification_big)
val builder = NotificationCompat.Builder(context, channelId)
val smallIcon = IconCompat.createFromIcon(
context, Icon.createWithResource(
context, R.mipmap.logo
)
)
smallIcon?.let {
builder.setSmallIcon(smallIcon) //设置状态栏内的小图标
}
val nfIntent = Intent(context, MainActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)
builder.setLargeIcon(
BitmapFactory.decodeResource(
context.resources,
R.mipmap.logo
)
)
builder.setContentTitle(context.resources.getString(R.string.app_name))
builder.setContentIntent(pendingIntent) //设置PendingIntent
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
builder.setAutoCancel(false)
builder.setOngoing(true)
builder.setPriority(NotificationCompat.PRIORITY_MAX) //优先级为:重要通知
builder.setWhen(System.currentTimeMillis())
builder.setCustomContentView(contentView)
builder.setCustomBigContentView(expendView)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
channel.lockscreenVisibility = 1
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
builder.setChannelId(channelId)
}
return builder.build()
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//监听媒体库变化
observerMediaContentObserver()
return super.onStartCommand(intent, flags, startId)
}
private fun startForeground() {
val notification = createPermanentNotification(applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
NOTIFICATION_PERMANENT_ID,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
} else {
startForeground(NOTIFICATION_PERMANENT_ID, notification)
}
isRunning = true
}
override fun onDestroy() {
isRunning = false
super.onDestroy()
}
override fun onCreate() {
if (!isRunning) {
startForeground()
isRunning = true
}
super.onCreate()
}
override fun onStartJob(params: JobParameters?): Boolean {
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
return false
}
private var mediaContentObserver: MediaContentObserver? = null
private fun observerMediaContentObserver() {
if (mediaContentObserver == null) {
LogEx.logDebug(TAG, "observerMediaContentObserver")
mediaContentObserver = MediaContentObserver(this)
mediaContentObserver?.let {
this.contentResolver.registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, it
)
}
}
}
}
\ No newline at end of file
......@@ -20,7 +20,6 @@ import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.helper.EventUtils
import com.base.pdfviewerscannerwhite.helper.MyApplication
import com.base.pdfviewerscannerwhite.helper.WeatherUtils
import com.base.pdfviewerscannerwhite.service.StayNotificationService.Companion.startStayNotification
import com.base.pdfviewerscannerwhite.ui.main.MainActivity
import com.base.pdfviewerscannerwhite.ui.permission.PermissionActivity
import com.base.pdfviewerscannerwhite.ui.set.SetLanguageActivity
......@@ -61,7 +60,7 @@ class SplashActivity : BaseActivity<ActivitySplash2Binding>(), SplashView {
)
}
splashPresenter = SplashPresenter(this)
startStayNotification()
// startStayNotification()
actionId = intent.extras?.getString("actionId") ?: ""
LogEx.logDebug(TAG, "actionId=$actionId")
......
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