android关闭手机偷录她人功能实现(退出程序依然录像)

最近的几个项目中,遇到了这个需求。正常情况下,我们使用相机录像的话,都是打开系统照相机进行操作;再或者就是使用SurfaceView进行绘制。
但是这样的话android就需要在页面上一直保持一个SurfaceView进行操作,那么怎么当程序进入后台时就没法操作了呀!怎么办呢?

项目中需求效果如下:

这里写图片描述

当然如果要录像那么SurfaceView是少不了的!那么就应该想到WindowManager来绘制桌面小控件的原理了!那么就可以保证这个SurfaceView一直存在了!再把宽高设置为1dp,就可以让用户视觉上有了后台录制的效果了!

一、首先我们需要一个SurfaceView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/small_window_layout"
    android:layout_width="1dip"
    android:layout_height="1dip"
    android:background="@drawable/bg_small"
    >
    <SurfaceView
        android:id="@+id/percent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textColor="#ffffff"
        />
</LinearLayout>

二、然后进行的操作就是生产这个小控件了:

   public FloatWindowSmallView(Context context) {
        super(context);
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater.from(context).inflate(R.layout.float_window_small, this);
        View view = findViewById(R.id.small_window_layout);
        viewWidth = view.getLayoutParams().width;
        viewHeight = view.getLayoutParams().height;
//        SurfaceView percentView = (SurfaceView) findViewById(R.id.percent);
//        percentView.setText(MyWindowManager.getUsedPercentValue(context));
    }



    /**
     * 将小悬浮窗的参数传入,用于更新小悬浮窗的位置。
     *
     * @param params 小悬浮窗的参数
     */
    public void setParams(WindowManager.LayoutParams params) {
        mParams = params;
    }

三、那桌面控件有了,下面当然就是使用WindowManager添加到桌面上了:

/**
     * 创建一个小悬浮窗。初始位置为屏幕的右部中间位置。
     *
     * @param context 必须为应用程序的Context.
     */
    public void createSmallWindow(Context context) {
        mContext = context;
        WindowManager windowManager = getWindowManager(context);
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        int screenHeight = windowManager.getDefaultDisplay().getHeight();
        if (smallWindow == null) {
            smallWindow = new FloatWindowSmallView(context);
            if (smallWindowParams == null) {
                smallWindowParams = new LayoutParams();
                smallWindowParams.type = LayoutParams.TYPE_PHONE;
                smallWindowParams.format = PixelFormat.RGBA_8888;
                smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | LayoutParams.FLAG_NOT_FOCUSABLE;
                smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
                smallWindowParams.width = FloatWindowSmallView.viewWidth;
                smallWindowParams.height = FloatWindowSmallView.viewHeight;
                smallWindowParams.x = screenWidth;
                smallWindowParams.y = screenHeight / 2;
            }
            smallWindow.setParams(smallWindowParams);
            windowManager.addView(smallWindow, smallWindowParams);

            mSurfaceview = (SurfaceView) smallWindow.findViewById(R.id.percent);
            SurfaceHolder holder = mSurfaceview.getHolder();
            holder.addCallback(this);
            // setType必须设置,要不出错.
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        }
    }

四、这个时候我们需要的SurfaceView就有了,那么,怎么在后台进行操作呢?自然而然就想到了Service了

在Service中执行桌面控件的操作:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        myWindowManager = new MyWindowManager();
        createWindow();
        return super.onStartCommand(intent, flags, startId);
    }


    private void createWindow() {

        // 当前界面是桌面,且没有悬浮窗显示,则创建悬浮窗。
        myWindowManager.removeSmallWindow(getApplicationContext());
        myWindowManager.createSmallWindow(getApplicationContext());

    }

五、在activity中对Service绑定,进行录像的操作

  conn = new MyServiceConn();
  bindService(intent, conn, BIND_AUTO_CREATE);


private class MyServiceConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            binder = (FloatWindowService.myServiceBinder) service;
            if (isVedio) {
                binder.startRecord();
            } else {
                binder.stopRecord();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
        }

    }

六、在Service中控制myWindowManager中的录像的开始和结束

public class myServiceBinder extends Binder {
        public void startRecord() {
            myWindowManager.startVideo();
        }

        public void stopRecord() {
            myWindowManager.stopVideo();
        }
    }

七、最后在myWindowManager进行录像操作就OK了。

 public void startVideo() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
        }
        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        if (camera != null) {
            camera.setDisplayOrientation(270);
            camera.unlock();
            mRecorder.setCamera(camera);
        }
        try {
            // 这两项需要放在setOutputFormat之前
            mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            // Set output file format
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

            // 这两项需要放在setOutputFormat之后
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

            mRecorder.setVideoSize(640, 480);
//                        mRecorder.setVideoSize(width, height);
            mRecorder.setVideoFrameRate(30);
            mRecorder.setVideoEncodingBitRate(3 * 1024 * 1024);
            mRecorder.setOrientationHint(90);
            //设置记录会话的最大持续时间(毫秒)
            mRecorder.setMaxDuration(7200 * 1000);
//                        mRecorder.setMaxDuration(Integer.MAX_VALUE);
            mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

            path = Environment.getExternalStorageDirectory().getAbsolutePath();
            if (path != null) {
                File dir = new File(path + "/DCIM/Camera");
                if (!dir.exists()) {
                    dir.mkdir();
                }
                videoTitle = System.currentTimeMillis() + ".mp4";
                path = dir + "/" + videoTitle;
                mRecorder.setOutputFile(path);
                mRecorder.prepare();
                mRecorder.start();
//                            mBtnStartStop.setText("录制结束");
                Log.i("LogUtils", "path=====>" + path);
            } else {
                Toast.makeText(mContext, "SD卡不存在...", Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopVideo() {
        try {
            mRecorder.stop();
            mRecorder.reset();
            mRecorder.release();
            mRecorder = null;
//                            mBtnStartStop.setText("开始录制视频");
            if (camera != null) {
                camera.release();
                camera = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mSurfaceHolder = surfaceHolder;
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
// 将holder,这个holder为开始在onCreate里面取得的holder,将它赋给mSurfaceHolder
        mSurfaceHolder = surfaceHolder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceview = null;
        mSurfaceHolder = null;
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
            Log.i("LogUtils", "surfaceDestroyed release mRecorder");
        }
        if (camera != null) {
            camera.release();
            camera = null;
        }

    }

记得配置权限等

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ddv.www.candidcamerademo">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.GET_TASKS" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".FloatWindowService" />
    </application>

</manifest>

demo下载地址:http://download.csdn.net/detail/huangxiaoguo1/9711893

有什么不足,望大家指教,谢谢….

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值