@@ -13,14 +13,12 @@ import androidx.annotation.RequiresApi
1313import androidx.core.app.NotificationCompat
1414import com.ven.assists.base.R
1515
16-
17- /* *
18- * 屏幕录制服务
19- */
2016class MPService : Service () {
2117
2218 companion object {
23- var onStartCommand: ((service: MPService , intent: Intent , flags: Int , startId: Int ) -> Unit )? = null
19+ var onStartCommand:
20+ ((service: MPService , intent: Intent , flags: Int , startId: Int ) -> Unit )? =
21+ null
2422 }
2523
2624 override fun onCreate () {
@@ -32,105 +30,33 @@ class MPService : Service() {
3230 return super .onStartCommand(intent, flags, startId)
3331 }
3432
35- private fun captureScreen (mediaProjection : MediaProjection ) {
36-
37- // val screenWidth = ScreenUtils.getScreenWidth()
38- // val screenHeight = ScreenUtils.getScreenHeight()
39- // val screenDensityDpi = ScreenUtils.getScreenDensityDpi()
40- //
41- // imageReader = ImageReader.newInstance(screenWidth, screenHeight, PixelFormat.RGBA_8888, 2)
42- //
43- // mediaProjection.createVirtualDisplay(
44- // "ScreenCapture",
45- // screenWidth,
46- // screenHeight,
47- // screenDensityDpi,
48- // DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
49- // imageReader?.surface,
50- // null,
51- // null
52- // )
53- }
54-
55-
56- // fun saveBitmap(): File? {
57- // val image: Image? = imageReader?.acquireLatestImage()
58- //
59- // try {
60- // val width = ScreenUtils.getScreenWidth()
61- // val height = ScreenUtils.getScreenHeight()
62- // val planes: Array<Image.Plane> = image?.planes ?: return null
63- // val buffer: ByteBuffer = planes[0].buffer
64- // val pixelStride: Int = planes[0].pixelStride
65- // val rowStride: Int = planes[0].rowStride
66- // val rowPadding = rowStride - pixelStride * width
67- // val bitmap = Bitmap.createBitmap(
68- // width + rowPadding / pixelStride,
69- // height,
70- // Bitmap.Config.ARGB_8888
71- // )
72- // bitmap.copyPixelsFromBuffer(buffer)
73- // val file = File(PathUtils.getInternalAppCachePath(), "${System.currentTimeMillis()}.png")
74- // val fileOutputStream = FileOutputStream(file)
75- // bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
76- // fileOutputStream.flush()
77- // fileOutputStream.close()
78- // image.close()
79- // return file
80- // } catch (e: Throwable) {
81- // return null
82- // } finally {
83- // image?.close()
84- // }
85- // }
86-
87- // fun toBitmap(): Bitmap? {
88- // val image: Image? = imageReader?.acquireLatestImage()
89- //
90- // return try {
91- // val width = ScreenUtils.getScreenWidth()
92- // val height = ScreenUtils.getScreenHeight()
93- // val planes: Array<Image.Plane> = image?.planes ?: return null
94- // val buffer: ByteBuffer = planes[0].buffer
95- // val pixelStride: Int = planes[0].pixelStride
96- // val rowStride: Int = planes[0].rowStride
97- // val rowPadding = rowStride - pixelStride * width
98- // val bitmap = Bitmap.createBitmap(
99- // width + rowPadding / pixelStride,
100- // height,
101- // Bitmap.Config.ARGB_8888
102- // )
103- // bitmap.copyPixelsFromBuffer(buffer)
104- // return bitmap
105- // } catch (e: Throwable) {
106- // null
107- // } finally {
108- // image?.close()
109- // }
110- // }
111-
11233 private fun initNotificationChannel () {
113- val channelId = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
114- createNotificationChannel()
115- } else {
116- " "
117- }
34+ val channelId =
35+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
36+ createNotificationChannel()
37+ } else {
38+ " "
39+ }
11840 val builder = NotificationCompat .Builder (this , channelId)
119- val notification = builder.setOngoing(true )
120- .setSmallIcon(R .mipmap.ic_launcher)
121- .setPriority(NotificationCompat .PRIORITY_MIN )
122- .setCategory(Notification .CATEGORY_SERVICE )
123- .build()
41+ val notification =
42+ builder.setOngoing(true )
43+ .setSmallIcon(R .mipmap.ic_launcher)
44+ .setPriority(NotificationCompat .PRIORITY_MIN )
45+ .setCategory(Notification .CATEGORY_SERVICE )
46+ .build()
12447 startForeground(1 , notification)
12548 }
12649
127- /* *
128- * 创建通知通道
129- */
50+ /* * 创建通知通道 */
13051 @RequiresApi(Build .VERSION_CODES .O )
13152 private fun createNotificationChannel (): String {
13253 val channelId = " mirror.hsl"
133- val chan = NotificationChannel (channelId, " ForegroundService" , NotificationManager .IMPORTANCE_NONE )
54+ val chan =
55+ NotificationChannel (
56+ channelId,
57+ " ForegroundService" ,
58+ NotificationManager .IMPORTANCE_NONE
59+ )
13460 chan.lightColor = Color .BLUE
13561 chan.lockscreenVisibility = Notification .VISIBILITY_PRIVATE
13662 val service = getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
@@ -145,4 +71,4 @@ class MPService : Service() {
14571 override fun onDestroy () {
14672 super .onDestroy()
14773 }
148- }
74+ }
0 commit comments