Skip to content

Instantly share code, notes, and snippets.

@mtsahakis
Last active October 17, 2018 18:58
Show Gist options
  • Save mtsahakis/5c709bbd9cf5074a377e6e3114c0e4b6 to your computer and use it in GitHub Desktop.
Save mtsahakis/5c709bbd9cf5074a377e6e3114c0e4b6 to your computer and use it in GitHub Desktop.
package com.mtsahakis.mediaprojectiondemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ScreenCaptureActivity extends Activity {
private static final int REQUEST_CODE = 100;
/****************************************** Activity Lifecycle methods ************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start projection
Button startButton = (Button)findViewById(R.id.startButton);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startProjection();
}
});
// stop projection
Button stopButton = (Button)findViewById(R.id.stopButton);
stopButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopProjection();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
startService(ScreenCaptureService.getStartIntent(this, resultCode, data));
}
}
}
/****************************************** UI Widget Callbacks *******************************/
private void startProjection() {
MediaProjectionManager mProjectionManager =
(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}
private void stopProjection() {
startService(ScreenCaptureService.getStopIntent(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment