Commit 0af249ec authored by mengcuiguang's avatar mengcuiguang

添加自启动

parent f1cc8b3c
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config" android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".ui.setting.SettingActivity"></activity> <activity android:name=".ui.setting.SettingActivity"></activity>
...@@ -20,7 +21,11 @@ ...@@ -20,7 +21,11 @@
<activity android:name=".ui.albumClean.AlbumCleanActivity" /> <activity android:name=".ui.albumClean.AlbumCleanActivity" />
<activity <activity
android:name=".ui.multiClean.MultiCleanActivity" android:name=".ui.multiClean.MultiCleanActivity"
android:launchMode="singleTop"> android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
...@@ -35,9 +40,7 @@ ...@@ -35,9 +40,7 @@
<activity android:name=".ui.login.LoginActivity" /> <activity android:name=".ui.login.LoginActivity" />
<activity android:name=".ui.splash.SplashActivity" /> <activity android:name=".ui.splash.SplashActivity" />
<activity android:name=".ui.NavigationActivity">
</activity>
<activity <activity
android:name=".ui.PreviewActivity" android:name=".ui.PreviewActivity"
android:launchMode="singleTop" android:launchMode="singleTop"
...@@ -54,6 +57,22 @@ ...@@ -54,6 +57,22 @@
android:resource="@xml/provider_paths" /> android:resource="@xml/provider_paths" />
</provider> </provider>
<!-- 保活唤醒 -->
<service
android:name=".service.ScreenService"
android:process=":screen" />
<service
android:name=".service.keep.KeepLiveManager$GrayInnerService"
android:process=":screen" />
<activity
android:name=".service.keep.PixelActivity"
android:excludeFromRecents="true"
android:exported="false"
android:finishOnTaskLaunch="false"
android:launchMode="singleInstance"
android:process=":screen"
android:theme="@style/LiveActivityStyle" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -37,7 +37,7 @@ class App : Application() { ...@@ -37,7 +37,7 @@ class App : Application() {
private fun initThirdConfig() { private fun initThirdConfig() {
// 屏幕适配 // 屏幕适配
initRudenessScreen() // initRudenessScreen()
// 生命周期 // 生命周期
LifeManager().install(this) LifeManager().install(this)
......
package com.mints.cleaner.manager
import android.annotation.TargetApi
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.mints.cleaner.R
import com.mints.cleaner.ui.multiClean.MultiCleanActivity
/**
* 通知管理
*/
class NotifyManager private constructor(private val service: Service) {
companion object {
const val CHINALL_ID = "MINTSCLEAN"
const val CHINALL_NAME = "清理"
private var _inst: NotifyManager? = null
@JvmStatic
fun getInstance(service: Service): NotifyManager? {
return if (_inst != null) {
_inst
} else {
_inst = NotifyManager(service)
_inst
}
}
}
/**
* 创建通知
*/
fun createNotifyDialog(): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(
CHINALL_ID,
CHINALL_NAME,
NotificationManager.IMPORTANCE_MAX
)
}
return NotificationCompat.Builder(
service,
CHINALL_ID
)
.setOngoing(true)
.setAutoCancel(true)
.setContentTitle("")
.setContentText("")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(remoteViews)
.setVibrate(longArrayOf(0))
.build()
}
/**
* 创建通知渠道
*/
@TargetApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(
channelId: String,
channelName: String,
importance: Int
) {
val channel = NotificationChannel(channelId, channelName, importance)
channel.enableVibration(false) //震动不可用
channel.setSound(null, null) //设置没有声
val notificationManager = service.getSystemService(
Context.NOTIFICATION_SERVICE
) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
/**
* 创建通知显示view
*/
private val remoteViews: RemoteViews
private get() {
val mainPendingIntent = PendingIntent.getActivity(
service,
0,
Intent(service, MultiCleanActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val remoteViews = RemoteViews(service.packageName, R.layout.view_remote)
remoteViews.setOnClickPendingIntent(R.id.notify_ic, mainPendingIntent)
return remoteViews
}
}
\ No newline at end of file
package com.mints.cleaner.service
import android.content.Intent
import com.mints.cleaner.service.keep.KeepliveService
/**
* 开幕保活
*/
class ScreenService : KeepliveService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val i = super.onStartCommand(intent, flags, startId)
// LogUtil.d("ScreenService",i.toString())
return i
}
}
\ No newline at end of file
package com.mints.cleaner.service.keep
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.IBinder
import com.mints.cleaner.manager.NotifyManager.Companion.getInstance
import com.mints.cleaner.util.LogUtil
/**
* 后台进程保活管理类
*/
class KeepLiveManager {
/**
* 1像素的透明Activity
*/
private var activity: PixelActivity? = null
/**
* 监听锁屏/解锁的广播(必须动态注册)
*/
private var lockReceiver: LockReceiver? = null
/**
* 传入1像素的透明Activity实例
*
* @param activity
*/
fun setKeepLiveActivity(activity: PixelActivity?) {
this.activity = activity
}
/**
* 注册锁屏/解锁广播
*
* @param context
*/
fun registerReceiver(context: Context) {
lockReceiver = LockReceiver()
val filter = IntentFilter()
filter.addAction(Intent.ACTION_SCREEN_ON)
filter.addAction(Intent.ACTION_SCREEN_OFF)
filter.addAction(Intent.ACTION_USER_PRESENT)
context.registerReceiver(lockReceiver, filter)
}
/**
* 注销锁屏/解锁广播
*
* @param context
*/
fun unRegisterReceiver(context: Context) {
if (lockReceiver != null) {
context.unregisterReceiver(lockReceiver)
}
}
/**
* 设置服务为前台服务
*
* @param service
*/
fun setServiceForeground(service: Service) {
service.startForeground(
GRAY_SERVICE_ID,
getInstance(service)!!.createNotifyDialog()
)
}
private fun startLiveActivity(context: Context) {
val intent = Intent(context, PixelActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
private fun destroyLiveActivity() {
if (activity != null) {
activity!!.finish()
}
}
internal inner class LockReceiver : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) {
when (intent.action) {
Intent.ACTION_SCREEN_OFF -> {
// 熄屏
startLiveActivity(context)
LogUtil.d("LockReceiver","熄屏 Intent.ACTION_SCREEN_OFF")
}
Intent.ACTION_SCREEN_ON -> {
// 亮屏
LogUtil.d("LockReceiver","亮屏 Intent.ACTION_SCREEN_ON")
}
Intent.ACTION_USER_PRESENT -> {
// 亮屏解锁
destroyLiveActivity()
}
}
}
}
/**
* 辅助Service
*/
class GrayInnerService : Service() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
startForeground(
GRAY_SERVICE_ID,
getInstance(this)!!.createNotifyDialog()
)
stopForeground(true)
stopSelf()
return super.onStartCommand(intent, flags, startId)
}
override fun onBind(intent: Intent): IBinder? {
return null
}
}
companion object {
/**
* 前台进程的NotificationId
*/
private const val GRAY_SERVICE_ID = 1002
/**
* 单例模式
*/
val instance = KeepLiveManager()
}
}
\ No newline at end of file
package com.mints.cleaner.service.keep
import android.app.Service
import android.content.Intent
import android.os.IBinder
open class KeepliveService : Service() {
override fun onCreate() {
super.onCreate()
KeepLiveManager.instance.registerReceiver(this)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
KeepLiveManager.instance.setServiceForeground(this)
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
KeepLiveManager.instance.unRegisterReceiver(this)
}
override fun onBind(intent: Intent): IBinder? {
return null
}
}
\ No newline at end of file
package com.mints.cleaner.service.keep
import android.os.Bundle
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
/**
* 一像素的Activity
*/
class PixelActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val window = window
window.setGravity(Gravity.LEFT or Gravity.TOP)
val params = window.attributes
params.x = 0
params.y = 0
params.height = 1
params.width = 1
window.attributes = params
KeepLiveManager.instance.setKeepLiveActivity(this)
}
}
package com.mints.cleaner.ui package com.mints.cleaner.ui
import android.content.Intent
import com.mints.cleaner.App
import com.mints.cleaner.R import com.mints.cleaner.R
import com.mints.cleaner.service.ScreenService
import com.mints.core.base.BaseActivity import com.mints.core.base.BaseActivity
class NavigationActivity : BaseActivity() { class NavigationActivity : BaseActivity() {
...@@ -8,6 +11,7 @@ class NavigationActivity : BaseActivity() { ...@@ -8,6 +11,7 @@ class NavigationActivity : BaseActivity() {
override fun getLayoutResId() = R.layout.activity_navigation override fun getLayoutResId() = R.layout.activity_navigation
override fun initView() { override fun initView() {
startService(Intent(App.CONTEXT, ScreenService::class.java))
} }
override fun initData() { override fun initData() {
......
...@@ -11,6 +11,7 @@ import com.mints.cleaner.adapter.multiClean.MultiCleanAdapter ...@@ -11,6 +11,7 @@ import com.mints.cleaner.adapter.multiClean.MultiCleanAdapter
import com.mints.cleaner.databinding.ActivityMultiCleanBinding import com.mints.cleaner.databinding.ActivityMultiCleanBinding
import com.mints.cleaner.model.bean.CleanHeaderTitle import com.mints.cleaner.model.bean.CleanHeaderTitle
import com.mints.cleaner.model.bean.FileBean import com.mints.cleaner.model.bean.FileBean
import com.mints.cleaner.service.ScreenService
import com.mints.cleaner.util.FileUtils import com.mints.cleaner.util.FileUtils
import com.mints.cleaner.util.FormatUtils import com.mints.cleaner.util.FormatUtils
import com.mints.cleaner.util.IntentUtils import com.mints.cleaner.util.IntentUtils
...@@ -20,13 +21,12 @@ import kotlinx.android.synthetic.main.activity_multi_clean.* ...@@ -20,13 +21,12 @@ import kotlinx.android.synthetic.main.activity_multi_clean.*
import java.io.File import java.io.File
import java.util.* import java.util.*
class MultiCleanActivity : BaseVMActivity(), MultiCleanAdapter.OnImageSelectListener, class MultiCleanActivity : BaseVMActivity(), MultiCleanAdapter.OnImageSelectListener,
MultiCleanAdapter.OnItemClickListener { MultiCleanAdapter.OnItemClickListener {
private val binding by binding<ActivityMultiCleanBinding>(R.layout.activity_multi_clean) private val binding by binding<ActivityMultiCleanBinding>(R.layout.activity_multi_clean)
private val dataList = arrayListOf<FileBean>() private val dataList = mutableListOf<FileBean>()
private val multiAdapter: MultiCleanAdapter by lazy { private val multiAdapter: MultiCleanAdapter by lazy {
MultiCleanAdapter( MultiCleanAdapter(
...@@ -118,6 +118,12 @@ class MultiCleanActivity : BaseVMActivity(), MultiCleanAdapter.OnImageSelectList ...@@ -118,6 +118,12 @@ class MultiCleanActivity : BaseVMActivity(), MultiCleanAdapter.OnImageSelectList
dataList.addAll(it) dataList.addAll(it)
} }
// Timer().schedule(1000) {
// runOnUiThread {
// multiAdapter.notifyDataSetChanged()
// }
// }
runOnUiThread { runOnUiThread {
multiAdapter.notifyDataSetChanged() multiAdapter.notifyDataSetChanged()
} }
...@@ -176,7 +182,9 @@ class MultiCleanActivity : BaseVMActivity(), MultiCleanAdapter.OnImageSelectList ...@@ -176,7 +182,9 @@ class MultiCleanActivity : BaseVMActivity(), MultiCleanAdapter.OnImageSelectList
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
clv_loading.hideLoading() clv_loading.hideLoading()
} }
override fun onImageSelect(selectCount: Int) { override fun onImageSelect(selectCount: Int) {
......
...@@ -24,7 +24,7 @@ class CleanLoadingView : LinearLayout { ...@@ -24,7 +24,7 @@ class CleanLoadingView : LinearLayout {
private val TIME_INTERVAL: Long = 1000 private val TIME_INTERVAL: Long = 1000
// 默认展示时间1.5秒 // 默认展示时间1.5秒
private val DEFAULT_LOAD_TIME: Long = 1500 private val DEFAULT_LOAD_TIME: Long = 2500
private var tvLoadingProgress: TextView? = null private var tvLoadingProgress: TextView? = null
private var ivLoading: ImageView? = null private var ivLoading: ImageView? = null
...@@ -77,8 +77,6 @@ class CleanLoadingView : LinearLayout { ...@@ -77,8 +77,6 @@ class CleanLoadingView : LinearLayout {
* 隐藏 * 隐藏
*/ */
fun hideLoading() { fun hideLoading() {
this.visibility = View.GONE
if (animator != null) { if (animator != null) {
animator!!.cancel() animator!!.cancel()
animator!!.removeAllListeners() animator!!.removeAllListeners()
...@@ -92,6 +90,8 @@ class CleanLoadingView : LinearLayout { ...@@ -92,6 +90,8 @@ class CleanLoadingView : LinearLayout {
circleAnimator!!.cancel() circleAnimator!!.cancel()
circleAnimator = null circleAnimator = null
} }
this.visibility = View.GONE
} }
@SuppressLint("ObjectAnimatorBinding") @SuppressLint("ObjectAnimatorBinding")
......
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/notify_root"
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/notify_ic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="40dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:text="自定义"
android:layout_height="wrap_content"/>
</LinearLayout>
...@@ -29,5 +29,9 @@ ...@@ -29,5 +29,9 @@
<item name="android:background">@color/gray_02</item> <item name="android:background">@color/gray_02</item>
</style> </style>
<style name="LiveActivityStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
</resources> </resources>
\ No newline at end of file
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