码迷,mamicode.com
首页 > 移动开发 > 详细

Android 7.1.1 系统截屏

时间:2017-09-19 23:04:26      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:hat   sage   port   计时   get   ibinder   androi   cep   bool   

frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java

技术分享

 

TakeScreenshotService.java

package com.android.systemui.screenshot;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.UserManager;
import android.util.Log;
import android.view.WindowManager;

public class TakeScreenshotService extends Service {
    private static final String TAG = "TakeScreenshotService";

    private static GlobalScreenshot mScreenshot;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            final Messenger callback = msg.replyTo;
            Runnable finisher = new Runnable() {
                @Override
                public void run() {
                    Message reply = Message.obtain(null, 1);
                    try {
                        callback.send(reply);
                    } catch (RemoteException e) {
                    }
                }
            };

            // If the storage for this user is locked, we have no place to store
            // the screenshot, so skip taking it instead of showing a misleading
            // animation and error notification.
            if (!getSystemService(UserManager.class).isUserUnlocked()) {
                Log.w(TAG, "Skipping screenshot because storage is locked!");
                post(finisher);
                return;
            }

            if (mScreenshot == null) {
                mScreenshot = new GlobalScreenshot(TakeScreenshotService.this);
            }

            switch (msg.what) {
                case WindowManager.TAKE_SCREENSHOT_FULLSCREEN:
                    mScreenshot.takeScreenshot(finisher, msg.arg1 > 0, msg.arg2 > 0);
                    break;
                case WindowManager.TAKE_SCREENSHOT_SELECTED_REGION:
                    mScreenshot.takeScreenshotPartial(finisher, msg.arg1 > 0, msg.arg2 > 0);
                    break;
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return new Messenger(mHandler).getBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        if (mScreenshot != null) mScreenshot.stopScreenshot();
        return true;
    }
}

 

 主要是创建一个 GlobalScreenshot 对象,通过GlobalScreenshot对象实现截屏,之所以将该类宿主在service里面是因为截屏可能产生耗时,这应该是设计时的理念。

当然完全可以由自己创建GlobalScreenshot对象,在线程里面实现截屏.

技术分享

 

Android 7.1.1 系统截屏

标签:hat   sage   port   计时   get   ibinder   androi   cep   bool   

原文地址:http://www.cnblogs.com/onelikeone/p/7554088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!