Android 10: android:foregroundServiceType="mediaProjection" not working with service
Asked Answered
I

2

1

Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

I am trying to capture the screen using MediaProjection API but my app is continuously crashing. I tried and giving this error:

   java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION

My service code:

  class RecordService : Service() {
        private var mServiceHandler: ServiceHandler? = null
        private var mMediaProjection: MediaProjection? = null
        private var mVirtualDisplay: VirtualDisplay? = null
        private var mMediaRecorder: MediaRecorder? = null
        private var resultCode = 0
        private var data: Intent? = null
        private var mScreenStateReceiver: BroadcastReceiver? = null
    var isInitialized=false
    
        private inner class ServiceHandler(looper: Looper?) : Handler(looper) {
            override fun handleMessage(msg: Message?) {
                if (resultCode == RESULT_OK) {
                    startRecording(resultCode, data)
                } else {
                }
            }
        }
    
        override fun onCreate() {

            // when the main app is being closed
            val notificationIntent = Intent(this, RecordService::class.java)
            val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
            val notification: Notification = Notification.Builder(this)
                .setContentTitle("DataRecorder")
                .setContentText("Your screen is being recorded and saved to your phone.")
                .setSmallIcon(R.drawable.ic_dialog_alert)
                .setContentIntent(pendingIntent)
                .setTicker("Tickertext")
                .build()
            isInitialized=true
            startForeground(ONGOING_NOTIFICATION_ID, notification)
  
            mScreenStateReceiver = MyBroadcastReceiver()
            val screenStateFilter = IntentFilter()
            screenStateFilter.addAction(Intent.ACTION_SCREEN_ON)
            screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF)
            screenStateFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED)
            registerReceiver(mScreenStateReceiver, screenStateFilter)
            val thread = HandlerThread(
                "ServiceStartArguments",
                THREAD_PRIORITY_BACKGROUND
            )
            thread.start()
            val mServiceLooper = thread.looper
            mServiceHandler = ServiceHandler(mServiceLooper)
        }
    
        override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
            Toast.makeText(this, "Starting recording service", Toast.LENGTH_SHORT).show()
            resultCode = intent.getIntExtra(EXTRA_RESULT_CODE, 0)
            data = intent.getParcelableExtra(EXTRA_DATA)
            check(!(resultCode == 0 || data == null)) { "Result code or data missing." }
            val msg = mServiceHandler!!.obtainMessage()
            msg.arg1 = startId
            mServiceHandler!!.sendMessage(msg)
            return START_REDELIVER_INTENT
        }
    
        private fun startRecording(resultCode: Int, data: Intent?) {
            val mProjectionManager =
                applicationContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
            mMediaRecorder = MediaRecorder()
            val metrics = DisplayMetrics()
            val wm =
                applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            wm.defaultDisplay.getRealMetrics(metrics)
            val mScreenDensity = metrics.densityDpi
            val displayWidth = metrics.widthPixels
            val displayHeight = metrics.heightPixels
            mMediaRecorder!!.setVideoSource(MediaRecorder.VideoSource.SURFACE)
            mMediaRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            mMediaRecorder!!.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
            mMediaRecorder!!.setVideoEncodingBitRate(8 * 1000 * 1000)
            mMediaRecorder!!.setVideoFrameRate(15)
            mMediaRecorder!!.setVideoSize(displayWidth, displayHeight)
            val videoDir =
                Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES)
                    .absolutePath
            val timestamp = System.currentTimeMillis()
            var orientation = "portrait"
            if (displayWidth > displayHeight) {
                orientation = "landscape"
            }
            val filePathAndName =
                videoDir + "/time_" + timestamp.toString() + "_mode_" + orientation + ".mp4"
            mMediaRecorder!!.setOutputFile(filePathAndName)
            try {
                mMediaRecorder!!.prepare()
            } catch (e: IllegalStateException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            if(isInitialized){
                mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data!!)
                val surface: Surface = mMediaRecorder!!.surface
                mVirtualDisplay = mMediaProjection!!.createVirtualDisplay(
                    "MainActivity",
                    displayWidth,
                    displayHeight,
                    mScreenDensity,
                    VIRTUAL_DISPLAY_FLAG_PRESENTATION,
                    surface,
                    null,
                    null
                )
                mMediaRecorder!!.start()
                Log.v(TAG, "Started recording")
    
            }
           }
    
        companion object {
            private const val TAG = "RECORDERSERVICE"
            private const val EXTRA_RESULT_CODE = "resultcode"
            private const val EXTRA_DATA = "data"
            private const val ONGOING_NOTIFICATION_ID = 23
  
        }
    }

I did add android:foregroundServiceType="mediaProjection" in Manifest as well but it is not working. I tried other similar questing but the issue is not resolved yet. Thanks

Intelligent answered 29/10, 2020 at 12:59 Comment(0)
C
1

Finally, I get the key: You must create NotificationChannel in onStartCommand firstly like below, otherwise you will get "FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION error".

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    LogUtil.d(TAG, " onStartCommand intent = " + intent);
    createNotificationChannel();
    
    //do MediaProjection things that you want
    return START_NOT_STICKY;
}


private void createNotificationChannel() {
    Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
    Intent nfIntent = new Intent(this, MainActivity.class); //点击后跳转的界面,可以设置跳转数据

    builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
        //.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
        .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
        .setContentText("is running......") // 设置上下文内容
        .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

    /*以下是对Android 8.0的适配*/
    //普通notification适配
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId("notification_id");
    }
    //前台服务notification适配
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(channel);
    }

    Notification notification = builder.build(); // 获取构建好的Notification
    notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
    startForeground(110, notification);
}
Christi answered 12/7, 2021 at 7:41 Comment(1)
This really solved my issue. of course also in combination with the permissionsOrbicular
A
0

are you adding this to your app manifest ?

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Accomplished answered 24/2, 2021 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.