在Android 4.4中以编程方式执行ScreenRecord命令

时间:2015-02-04 11:12:26

标签: android

我试图使用以下代码以编程方式记录屏幕:

private class SuTask extends AsyncTask<Boolean, Void, Boolean> {
    private final byte[] mCommand;

    public SuTask(byte[] command) {
        super();
        this.mCommand = command;
    }

    @Override
    protected Boolean doInBackground(Boolean... booleans) {
        try {
            Process sh = Runtime.getRuntime().exec("su", null, null);
            OutputStream outputStream = sh.getOutputStream();
            outputStream.write(mCommand);
            outputStream.flush();
            outputStream.close();

            final NotificationManager notificationManager = (NotificationManager) mContext
                    .getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(RUNNING_NOTIFICATION_ID,
                    createRunningNotification(mContext));

            sh.waitFor();
            return true;

        } catch (InterruptedException e) {
            e.printStackTrace();
            Toast.makeText(mContext,"error",
                    Toast.LENGTH_LONG).show();

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(mContext,"error",
                    Toast.LENGTH_LONG).show();
        }

        return false;
    }

    @Override
    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        if (bool) {
            final NotificationManager notificationManager = (NotificationManager) mContext
                    .getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(RUNNING_NOTIFICATION_ID);

            File file = new File(Environment.getExternalStorageDirectory().toString() 
                    + "/recording.mp4");
            notificationManager.notify(FINISHED_NOTIFICATION_ID,
                    createFinishedNotification(mContext, file));

        }

    }

    @SuppressLint("NewApi")
    private Notification createRunningNotification(Context context) {
        Notification.Builder mBuilder = new Notification.Builder(
                context)
                .setSmallIcon(android.R.drawable.stat_notify_sdcard)
                .setContentTitle(
                        context.getResources().getString(
                                R.string.app_name))
                .setContentText("Recording Running")
                .setTicker("Recording Running")
                .setPriority(Integer.MAX_VALUE).setOngoing(true);

        return mBuilder.build();
    }

    @SuppressLint("NewApi")
    private Notification createFinishedNotification(Context context,
            File file) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "video/mp4");

        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder mBuilder = new Notification.Builder(
                context)
                .setSmallIcon(android.R.drawable.stat_notify_sdcard)
                .setContentTitle(
                        context.getResources().getString(
                                R.string.app_name))
                .setContentText("Recording Finished")
                .setTicker("Recording Finished")
                .setContentIntent(pendingIntent).setOngoing(false)
                .setAutoCancel(true);

        return mBuilder.build();
    }
}

我在Button onclicklistener中调用了这个AsyncTask:

StringBuilder stringBuilder = new StringBuilder(
                        "/system/bin/screenrecord");
               /**
                    stringBuilder.append(" --size ")
                            .append("400").append("x")
                            .append("400");
                    stringBuilder.append(" --bit-rate ").append("20");
                    stringBuilder.append(" --time-limit ").append("180");*/

                // TODO User definable location.
                stringBuilder
                        .append(" ")
                        .append(Environment.getExternalStorageDirectory().toString()).append("/recording.mp4");
                Log.d("TAG", "comamnd: " + stringBuilder.toString());
                Log.d("TAG", "PATH: " + Environment.getExternalStorageDirectory());
                try {
                    new SuTask(stringBuilder.toString().getBytes("ASCII"))
                            .execute();

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    Log.e("TAG", "error: " + e.getMessage());
                }

但是当我运行它时,通知会显示“正在运行”,然后在几秒钟之后它会显示“已完成”。当我点击通知时,它说它无法播放视频。我还验证了SD卡中的视频文件,但是那里没有视频文件。

我将非常感谢那些现在可以帮助我的人。

更新:我尝试从命令提示符运行screenrecord,它的工作原理。但是,当我在手机中安装其他屏幕录制应用程序以测试其是否正常工作时,我总是收到错误:确保SD卡未被锁定/不可写入。

0 个答案:

没有答案