Recently, a function of the company's screen recording project is required. It has a function of suspending windows. If you click the suspension window button, you can directly record the screen. The demand is very simple. But if you encounter a very painful problem, you must use startActivityForResult if you want to request the android's recording authority. If you have a problem with the desktop request directly, that is, when the suspension window is on the desktop, click the suspension window. It would be very unfriendly to evoke app in the background, and all kinds of information were not found on the Internet. At last, I thought of a way to create a new activity with application authority, and set the theme to be transparent. However, I still couldn't do it. Finally, another wave of activity was added to a singleton startup mode. Finally, I posted PermissionActiv. It's code, followed by github's complete code.
Manifest manifest file
<activity android:name=".activity.PermissionActivity" android:launchMode="singleInstance" android:theme="@style/NoActivity"> </activity>
PermissionActivity
class PermissionActivity : Activity() { private var projectionManager: MediaProjectionManager? = null /** Screen Recording Control */ private var mRecordService: RecordService.RecordBinder? = null /** Service binding callback */ private var mServiceConnection: ServiceConnection = object : ServiceConnection { override fun onServiceDisconnected(name: ComponentName?) { mRecordService = null Log.d("record","Service deregulation") } override fun onServiceConnected(name: ComponentName, service: IBinder) { mRecordService = service as RecordService.RecordBinder Log.d("record","Service binding") } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) projectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager val intent = Intent(this, RecordService::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE) val captureIntent = projectionManager?.createScreenCaptureIntent() startActivityForResult(captureIntent, RECORD_REQUEST_CODE) } override fun onDestroy() { super.onDestroy() unbindService(mServiceConnection) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { mRecordService?.onRequestResult(requestCode, resultCode, data) finish() } }
Transparent theme
<style name="NoActivity" parent="android:Theme.Translucent.NoTitleBar.Fullscreen"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> </style>
If this article helps you, please give me a compliment.
Brief Book Address: https://www.jianshu.com/p/a10dc202faa0