Java Code Examples for android.media.projection.MediaProjectionManager#getMediaProjection()

The following examples show how to use android.media.projection.MediaProjectionManager#getMediaProjection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScreenRecorderService.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        createNotificationChannel();
        //Android Q 存在兼容性问题
        MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) intent.getParcelableExtra("data"));
        ColorPickManager.getInstance().setMediaProjection(mediaProjection);
        if (ColorPickManager.getInstance().getColorPickerDokitView() != null) {
            ColorPickManager.getInstance().getColorPickerDokitView().onScreenServiceReady();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return super.onStartCommand(intent, flags, startId);
}
 
Example 2
Source File: ImageCapture.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
public void init(Context context, Bundle bundle, ColorPickerDokitView colorPickerDokitView) throws Exception {
    this.mColorPickerDokitView = colorPickerDokitView;
    PackageManager packageManager = DoraemonKit.APPLICATION.getPackageManager();
    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(AppUtils.getAppPackageName(), 0);
    //适配Android Q
    if (applicationInfo.targetSdkVersion >= 29) {
        if (ColorPickManager.getInstance().getMediaProjection() != null) {
            colorPickerDokitView.onScreenServiceReady();
        } else {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    Intent intent = new Intent(context, ScreenRecorderService.class);
                    intent.putExtra("data", bundle.getParcelable("data"));
                    context.startForegroundService(intent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else {
        mMediaProjectionManager = (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        if (mMediaProjectionManager != null) {
            mMediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) bundle.getParcelable("data"));
            initImageRead(mMediaProjection);
        }
    }
}
 
Example 3
Source File: MediaProjectionHelper.java    From loco-answers with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get media projection media projection.
 *
 * @param context the context
 * @return the media projection
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected static MediaProjection getMediaProjection(Context context){
    MediaProjectionManager mgr= (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
    if(mMediaProjection!=null){
        mMediaProjection.stop();
        mMediaProjection=null;
    }
    mMediaProjection = mgr.getMediaProjection(Activity.RESULT_OK, (Intent) screenshotPermission.clone());
    return mMediaProjection;
}
 
Example 4
Source File: ScreenRecodeService.java    From pc-android-controller-android with Apache License 2.0 5 votes vote down vote up
private void initScreenManager() {
    L.d("初始化录屏。。。");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mMediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);

        mMediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK,
                mCapturePermissionIntent);
    }
    startDisplayManager();
    new Thread(new EncoderWorker()).start();
}
 
Example 5
Source File: MainActivity.java    From habpanelviewer with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Constants.REQUEST_PICK_APPLICATION && resultCode == RESULT_OK) {
        startActivity(data);
    } else if (requestCode == Constants.REQUEST_MEDIA_PROJECTION) {

        boolean allowCapture = resultCode == RESULT_OK;
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        if (prefs.getBoolean(Constants.PREF_CAPTURE_SCREEN_ENABLED, false) != allowCapture) {
            SharedPreferences.Editor editor1 = prefs.edit();
            editor1.putBoolean(Constants.PREF_CAPTURE_SCREEN_ENABLED, allowCapture);
            editor1.apply();
        }

        if (resultCode == RESULT_OK) {
            MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
            MediaProjection projection = projectionManager.getMediaProjection(RESULT_OK, data);

            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            Point size = new Point();
            getWindowManager().getDefaultDisplay().getSize(size);

            if (mCapturer == null) {
                mCapturer = new ScreenCapturer(projection, size.x, size.y, metrics.densityDpi);
            }
        }
    } else if (requestCode == Constants.REQUEST_VALIDATE) {
        if (resultCode == Activity.RESULT_CANCELED) {
            UiUtil.showButtonDialog(this, null,
                    getString(R.string.prefsDisabledMissingPermissions),
                    android.R.string.ok, (dialogInterface, i) -> onStart(), -1, null);
        } else {
            onStart();
        }
    }
}
 
Example 6
Source File: Screenshotter.java    From RelaxFinger with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Takes the screenshot of whatever currently is on the default display.
 * @param resultCode The result code returned by the request for accessing MediaProjection permission
 * @param data The intent returned by the same request
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public Screenshotter takeScreenshot(Context context, int resultCode, Intent data, final ScreenshotCallback cb) {
    this.context = context;
    this.cb = cb;
    this.resultCode = resultCode;
    this.data = data;

    WindowManager windowManager = (WindowManager) MyApplication.getApplication().getSystemService(Context.WINDOW_SERVICE);

    width = windowManager.getDefaultDisplay().getWidth();
    height = windowManager.getDefaultDisplay().getHeight();
    DisplayMetrics metrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(metrics);
    mScreenDensity = metrics.densityDpi;

    mImageReader = ImageReader.newInstance(width, height, 0x1,2);
    MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) context
            .getSystemService(Context.MEDIA_PROJECTION_SERVICE);
    mMediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
    try {
        virtualDisplay = mMediaProjection.createVirtualDisplay("Screenshotter",
                width, height, mScreenDensity,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                mImageReader.getSurface(), null, null);
        mImageReader.setOnImageAvailableListener(Screenshotter.this, null);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return this;
}
 
Example 7
Source File: RecorderService.java    From screenrecorder with GNU Affero General Public License v3.0 4 votes vote down vote up
private void startRecording() {
    //Initialize MediaRecorder class and initialize it with preferred configuration
    mMediaRecorder = new MediaRecorder();
    initRecorder();

    //Set Callback for MediaProjection
    mMediaProjectionCallback = new MediaProjectionCallback();
    MediaProjectionManager mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

    //Initialize MediaProjection using data received from Intent
    mMediaProjection = mProjectionManager.getMediaProjection(result, data);
    mMediaProjection.registerCallback(mMediaProjectionCallback, null);

    /* Create a new virtual display with the actual default display
             * and pass it on to MediaRecorder to start recording */
    mVirtualDisplay = createVirtualDisplay();
    try {
        mMediaRecorder.start();

        //If floating controls is enabled, start the floating control service and bind it here
        if (useFloatingControls) {
            Intent floatinControlsIntent = new Intent(this, FloatingControlService.class);
            startService(floatinControlsIntent);
            bindService(floatinControlsIntent,
                    serviceConnection, BIND_AUTO_CREATE);
        }

        //Set the state of the recording
        if (isBound)
            floatingControlService.setRecordingState(Const.RecordingState.RECORDING);
        isRecording = true;

        //Send a broadcast receiver to the plugin app to enable show touches since the recording is started
        if (showTouches) {
            Intent TouchIntent = new Intent();
            TouchIntent.setAction("com.orpheusdroid.screenrecorder.SHOWTOUCH");
            TouchIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            sendBroadcast(TouchIntent);
        }
        Toast.makeText(this, R.string.screen_recording_started_toast, Toast.LENGTH_SHORT).show();
    } catch (IllegalStateException e) {
        Log.d(Const.TAG, "Mediarecorder reached Illegal state exception. Did you start the recording twice?");
        Toast.makeText(this, R.string.recording_failed_toast, Toast.LENGTH_SHORT).show();
        isRecording = false;
    }

            /* Add Pause action to Notification to pause screen recording if the user's android version
             * is >= Nougat(API 24) since pause() isnt available previous to API24 else build
             * Notification with only default stop() action */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        //startTime is to calculate elapsed recording time to update notification during pause/resume
        startTime = System.currentTimeMillis();
        Intent recordPauseIntent = new Intent(this, RecorderService.class);
        recordPauseIntent.setAction(Const.SCREEN_RECORDING_PAUSE);
        PendingIntent precordPauseIntent = PendingIntent.getService(this, 0, recordPauseIntent, 0);
        NotificationCompat.Action action = new NotificationCompat.Action(android.R.drawable.ic_media_pause,
                getString(R.string.screen_recording_notification_action_pause), precordPauseIntent);

        //Start Notification as foreground
        startNotificationForeGround(createRecordingNotification(action).build(), Const.SCREEN_RECORDER_NOTIFICATION_ID);
    } else
        startNotificationForeGround(createRecordingNotification(null).build(), Const.SCREEN_RECORDER_NOTIFICATION_ID);
}