Skip to content

Instantly share code, notes, and snippets.

@mtsahakis
Created July 7, 2017 18:28
Show Gist options
  • Save mtsahakis/a46cafb8991af18a069e50758c29aab7 to your computer and use it in GitHub Desktop.
Save mtsahakis/a46cafb8991af18a069e50758c29aab7 to your computer and use it in GitHub Desktop.
Demo activity for question asked in https://github.com/mtsahakis/MediaProjectionDemo/issues/7
package com.mtsahakis.activity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
@TargetApi(21)
public class ScreenCaptureActivity extends Activity {
private static final int REQUEST_CODE_SCREEN_CAPTURE = 100;
private static final String STATE_RESULT_CODE = "result_code";
private static final String STATE_RESULT_DATA = "result_data";
private int mResultCode;
private Intent mResultData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mResultCode = savedInstanceState.getInt(STATE_RESULT_CODE);
mResultData = savedInstanceState.getParcelable(STATE_RESULT_DATA);
} else {
MediaProjectionManager projectionManager = (MediaProjectionManager) getApplicationContext().
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(), REQUEST_CODE_SCREEN_CAPTURE);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mResultData != null) {
outState.putInt(STATE_RESULT_CODE, mResultCode);
outState.putParcelable(STATE_RESULT_DATA, mResultData);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_SCREEN_CAPTURE) {
if(resultCode == Activity.RESULT_OK) {
mResultCode = resultCode;
mResultData = data;
onScreenCaptureAllowed(resultCode, data);
} else {
Log.d("ScreenCaptureActivity", "User cancelled screen capture.");
}
}
finish();
}
private void onScreenCaptureAllowed(int resultCode, Intent data) {
Log.d("ScreenCaptureActivity", "User allowed screen capture.");
// Kick off media projection with result code and data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment