Problem: I have a screenshot app that uses a floating overlay service for controls, and screen cast API Media Project Manager to get access to the screen. Sometimes when a device is low on memory Android restarts the service, and I lose my media projection.
The only way I know of to reacquire a new media projection is to re-open an Activity that requests the permissions, and that would be the end of it, except for one problem. Certain apps, particularly games, seem to listen for when they lose the foreground process, and pause, or otherwise reset. This is annoying.
Here's my ideal scenerio. Service opens, if the user has selected " checked don't ask me again" in permission request dialog, it gets the media projection in a way that does not disturb the current foreground activity.
How do I get a media projection manager without disturbing the current foreground process?
Is there either a way to get media projection from a straight service, or a way to open activity in the background from a service?
Currently I use this code in Activity to get the MediaProjectionManager
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void getScreenShotPermission() {
if (isLollipopOrNewer) {
mediaProjectionManager = (MediaProjectionManager) getContext().getSystemService(MEDIA_PROJECTION_SERVICE);
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 1);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
this.finish();
}
}
}