在接收行情数据等特殊条件下,必须保证服务能够连续执行。可按以下步骤执行:(仅限于HUAWEI P10 Plus 及 HORNOR X10,其它机型未测试)
- 在 AndroidManifest.xml 中增加权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
- 在 MainActivity#onCreate() 中设置服务为前台服务:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val intent = Intent(this, FjztService::class.java)
startForegroundService(intent)
}
- 在 TestService#onCreate() 中添加代码:
class TestService : Service() {
private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var serviceThread: Thread
override fun onCreate() {
super.onCreate()
Log.i(TAG, "onCreate: ")
createChannels()
startForeground()
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TestService::class.java.name)
wakeLock.acquire()
serviceThread = thread {
while (true) {
showNotification(...)
try {
TimeUnit.MINUTES.sleep(1)
} catch (e: InterruptedException) {
break
}
}
Log.i(TAG, "loopThread exit.")
}
}
private fun createChannels() {
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channelDescription = "channel description"
val channelImportance = NotificationManager.IMPORTANCE_DEFAULT
NotificationChannel("channel-0 ID", "channel-0 Name", channelImportance).also {
it.description = channelDescription
it.enableLights(true)
manager.createNotificationChannel(it)
}
NotificationChannel("channel-1 ID", "channel-1 Name", channelImportance).also {
it.description = channelDescription
it.enableLights(true)
manager.createNotificationChannel(it)
}
NotificationChannel("channel-2 ID", "channel-2 Name", channelImportance).also {
it.description = channelDescription
it.enableLights(true)
it.enableVibration(true)
it.vibrationPattern = longArrayOf(200, 200)
manager.createNotificationChannel(it)
}
}
private var notifyIdx = 1
private fun startForeground() {
val notification = Notification.Builder(this.applicationContext, "channel-0 ID")
.setContentText("ContentText: service start foreground.")
.setContentTitle("ContentTitle: service start foreground")
.setSmallIcon(R.drawable.ic_dialog_info)
.build()
startForeground(notifyIdx, notification)
}
private fun showNotification(...) {
val intent = Intent(this, MainActivity::class.java).also {
it.putExtra("param1", param1)
it.putExtra("param2", param2)
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notification = Notification.Builder(this.applicationContext, "channel-0 ID")
.setContentText("...")
.setContentTitle("...")
.setSmallIcon(R.drawable.ic_dialog_info)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build()
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.notify(notifyIdx, notification)
}
override fun onDestroy() {
super.onDestroy()
serviceThread.interrupt()
}
}
- 进入手机系统设置关闭高耗电提醒
- 设置应用启动管理
- 进入运行应用列表,下拉并松开
|