android 截圖功能源碼解析

一般沒有修改rom的android原生系統(tǒng)截圖功能的組合鍵是音量減+開機(jī)鍵愧旦;今天我們從源碼角度來分析截圖功能是如何在源碼中實現(xiàn)的铛只。

在android系統(tǒng)中埠胖,由于我們的每一個Android界面都是一個Activity,而界面的顯示都是通過Window對象實現(xiàn)的淳玩,每個Window對象實際上都是PhoneWindow的實例直撤,而每個PhoneWindow對象都對應(yīng)一個PhoneWindowManager對象,當(dāng)我們在Activity界面執(zhí)行按鍵操作的時候蜕着,在將按鍵的處理操作分發(fā)到App之前谋竖,首先會回調(diào)PhoneWindowManager中的dispatchUnhandledKey方法,該方法主要用于執(zhí)行當(dāng)前App處理按鍵之前的操作承匣,我們具體看一下該方法的實現(xiàn)蓖乘。

/** {@inheritDoc} */
    @Override
    public KeyEvent dispatchUnhandledKey(WindowState win, KeyEvent event, int policyFlags) {
        ...
        KeyEvent fallbackEvent = null;
        if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
            final KeyCharacterMap kcm = event.getKeyCharacterMap();
            final int keyCode = event.getKeyCode();
            final int metaState = event.getMetaState();
            final boolean initialDown = event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0;

            // Check for fallback actions specified by the key character map.
            final FallbackAction fallbackAction;
            if (initialDown) {
                fallbackAction = kcm.getFallbackAction(keyCode, metaState);
            } else {
                fallbackAction = mFallbackActions.get(keyCode);
            }

            if (fallbackAction != null) {
                ...
                final int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK;
                fallbackEvent = KeyEvent.obtain(
                        event.getDownTime(), event.getEventTime(),
                        event.getAction(), fallbackAction.keyCode,
                        event.getRepeatCount(), fallbackAction.metaState,
                        event.getDeviceId(), event.getScanCode(),
                        flags, event.getSource(), null);

                if (!interceptFallback(win, fallbackEvent, policyFlags)) {
                    fallbackEvent.recycle();
                    fallbackEvent = null;
                }

                if (initialDown) {
                    mFallbackActions.put(keyCode, fallbackAction);
                } else if (event.getAction() == KeyEvent.ACTION_UP) {
                    mFallbackActions.remove(keyCode);
                    fallbackAction.recycle();
                }
            }
        }

        ...
        return fallbackEvent;
    }

這里我們關(guān)注一下方法體中調(diào)用的:interceptFallback方法,通過調(diào)用該方法將處理按鍵的操作下發(fā)到該方法中韧骗,我們繼續(xù)看一下該方法的實現(xiàn)邏輯嘉抒。

private boolean interceptFallback(WindowState win, KeyEvent fallbackEvent, int policyFlags) {
        int actions = interceptKeyBeforeQueueing(fallbackEvent, policyFlags);
        if ((actions & ACTION_PASS_TO_USER) != 0) {
            long delayMillis = interceptKeyBeforeDispatching(
                    win, fallbackEvent, policyFlags);
            if (delayMillis == 0) {
                return true;
            }
        }
        return false;
    }

然后我們看到在interceptFallback方法中我們調(diào)用了interceptKeyBeforeQueueing方法,通過閱讀我們我們知道該方法主要實現(xiàn)了對截屏按鍵的處理流程袍暴,這樣我們繼續(xù)看一下interceptKeyBeforeWueueing方法的處理:

@Override
    public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
        if (!mSystemBooted) {
            // If we have not yet booted, don't let key events do anything.
            return 0;
        }

        ...
        // Handle special keys.
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {
                if (mUseTvRouting) {
                    // On TVs volume keys never go to the foreground app
                    result &= ~ACTION_PASS_TO_USER;
                }
                if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                    if (down) {
                        if (interactive && !mScreenshotChordVolumeDownKeyTriggered
                                && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                            mScreenshotChordVolumeDownKeyTriggered = true;
                            mScreenshotChordVolumeDownKeyTime = event.getDownTime();
                            mScreenshotChordVolumeDownKeyConsumed = false;
                            cancelPendingPowerKeyAction();
                            interceptScreenshotChord();
                        }
                    } else {
                        mScreenshotChordVolumeDownKeyTriggered = false;
                        cancelPendingScreenshotChordAction();
                    }
                }
                ...

        return result;
    }

可以發(fā)現(xiàn)這里首先判斷當(dāng)前系統(tǒng)是否已經(jīng)boot完畢些侍,若尚未啟動完畢,則所有的按鍵操作都將失效政模,若啟動完成娩梨,則執(zhí)行后續(xù)的操作,這里我們只是關(guān)注音量減少按鍵和電源按鍵組合的處理事件览徒。另外這里多說一句想安卓系統(tǒng)的HOME按鍵事件狈定,MENU按鍵事件,進(jìn)程列表按鍵事件等等都是在這里實現(xiàn)的习蓬,后續(xù)中我們會陸續(xù)介紹這方面的內(nèi)容纽什。

回到我們的interceptKeyBeforeQueueing方法,當(dāng)我用按下音量減少按鍵的時候回進(jìn)入到:case KeyEvent.KEYCODE_VOLUME_MUTE分支并執(zhí)行相應(yīng)的邏輯躲叼,然后同時判斷用戶是否按下了電源鍵芦缰,若同時按下了電源鍵,則執(zhí)行:

if (interactive && !mScreenshotChordVolumeDownKeyTriggered
                                && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                            mScreenshotChordVolumeDownKeyTriggered = true;
                            mScreenshotChordVolumeDownKeyTime = event.getDownTime();
                            mScreenshotChordVolumeDownKeyConsumed = false;
                            cancelPendingPowerKeyAction();
                            interceptScreenshotChord();
                        }

可以發(fā)現(xiàn)這里的interceptScreenshotChrod方法就是系統(tǒng)準(zhǔn)備開始執(zhí)行截屏操作的開始枫慷,我們繼續(xù)看一下interceptcreenshotChord方法的實現(xiàn)让蕾。

private void interceptScreenshotChord() {
        if (mScreenshotChordEnabled
                && mScreenshotChordVolumeDownKeyTriggered && mScreenshotChordPowerKeyTriggered
                && !mScreenshotChordVolumeUpKeyTriggered) {
            final long now = SystemClock.uptimeMillis();
            if (now <= mScreenshotChordVolumeDownKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS
                    && now <= mScreenshotChordPowerKeyTime
                            + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS) {
                mScreenshotChordVolumeDownKeyConsumed = true;
                cancelPendingPowerKeyAction();

                mHandler.postDelayed(mScreenshotRunnable, getScreenshotChordLongPressDelay());
            }
        }
    }

在方法體中我們最終會執(zhí)行發(fā)送一個延遲的異步消息浪规,請求執(zhí)行截屏的操作而這里的延時時間,若當(dāng)前輸入框是打開狀態(tài)探孝,則延時時間為輸入框關(guān)閉時間加上系統(tǒng)配置的按鍵超時時間笋婿,若當(dāng)前輸入框沒有打開則直接是系統(tǒng)配置的按鍵超時處理時間,可看一下getScreenshotChordLongPressDelay方法的具體實現(xiàn)顿颅。

private long getScreenshotChordLongPressDelay() {
        if (mKeyguardDelegate.isShowing()) {
            // Double the time it takes to take a screenshot from the keyguard
            return (long) (KEYGUARD_SCREENSHOT_CHORD_DELAY_MULTIPLIER *
                    ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
        }
        return ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout();
    }

回到我們的interceptScreenshotChord方法缸濒,發(fā)送了異步消息之后系統(tǒng)最終會被我們發(fā)送的Runnable對象的run方法執(zhí)行;這樣我們看一下Runnable類型的mScreenshotRunnable的run方法的實現(xiàn):

private final Runnable mScreenshotRunnable = new Runnable() {
        @Override
        public void run() {
            takeScreenshot();
        }
    };

好吧粱腻,方法體中并未執(zhí)行其他操作庇配,直接就是調(diào)用了takeScreenshot方法,這樣我們繼續(xù)看一下takeScreenshot方法的實現(xiàn)绍些。

private void takeScreenshot() {
        synchronized (mScreenshotLock) {
            if (mScreenshotConnection != null) {
                return;
            }
            ComponentName cn = new ComponentName("com.android.systemui",
                    "com.android.systemui.screenshot.TakeScreenshotService");
            Intent intent = new Intent();
            intent.setComponent(cn);
            ServiceConnection conn = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    synchronized (mScreenshotLock) {
                        if (mScreenshotConnection != this) {
                            return;
                        }
                        Messenger messenger = new Messenger(service);
                        Message msg = Message.obtain(null, 1);
                        final ServiceConnection myConn = this;
                        Handler h = new Handler(mHandler.getLooper()) {
                            @Override
                            public void handleMessage(Message msg) {
                                synchronized (mScreenshotLock) {
                                    if (mScreenshotConnection == myConn) {
                                        mContext.unbindService(mScreenshotConnection);
                                        mScreenshotConnection = null;
                                        mHandler.removeCallbacks(mScreenshotTimeout);
                                    }
                                }
                            }
                        };
                        msg.replyTo = new Messenger(h);
                        msg.arg1 = msg.arg2 = 0;
                        if (mStatusBar != null && mStatusBar.isVisibleLw())
                            msg.arg1 = 1;
                        if (mNavigationBar != null && mNavigationBar.isVisibleLw())
                            msg.arg2 = 1;
                        try {
                            messenger.send(msg);
                        } catch (RemoteException e) {
                        }
                    }
                }
                @Override
                public void onServiceDisconnected(ComponentName name) {}
            };
            if (mContext.bindServiceAsUser(
                    intent, conn, Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
                mScreenshotConnection = conn;
                mHandler.postDelayed(mScreenshotTimeout, 10000);
            }
        }
    }

可以發(fā)現(xiàn)這里通過反射機(jī)制創(chuàng)建了一個TakeScreenshotService對象然后調(diào)用了bindServiceAsUser捞慌,這樣就創(chuàng)建了TakeScreenshotService服務(wù)并在服務(wù)創(chuàng)建之后發(fā)送了一個異步消息。好了柬批,我們看一下TakeScreenshotService的實現(xiàn)邏輯卿闹。

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) {
            switch (msg.what) {
                case 1:
                    final Messenger callback = msg.replyTo;
                    if (mScreenshot == null) {
                        mScreenshot = new GlobalScreenshot(TakeScreenshotService.this);
                    }
                    mScreenshot.takeScreenshot(new Runnable() {
                        @Override public void run() {
                            Message reply = Message.obtain(null, 1);
                            try {
                                callback.send(reply);
                            } catch (RemoteException e) {
                            }
                        }
                    }, msg.arg1 > 0, msg.arg2 > 0);
            }
        }
    };

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

可以發(fā)現(xiàn)在在TakeScreenshotService類的定義中有一個Handler成員變量,而我們在啟動TakeScreentshowService的時候回發(fā)送一個異步消息萝快,這樣就會執(zhí)行mHandler的handleMessage方法,然后在handleMessage方法中我們創(chuàng)建了一個GlobalScreenshow對象著角,然后執(zhí)行了takeScreenshot方法揪漩,好吧,繼續(xù)看一下takeScreentshot方法的執(zhí)行邏輯吏口。

/**
     * Takes a screenshot of the current display and shows an animation.
     */
    void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible) {
        // We need to orient the screenshot correctly (and the Surface api seems to take screenshots
        // only in the natural orientation of the device :!)
        mDisplay.getRealMetrics(mDisplayMetrics);
        float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};
        float degrees = getDegreesForRotation(mDisplay.getRotation());
        boolean requiresRotation = (degrees > 0);
        if (requiresRotation) {
            // Get the dimensions of the device in its native orientation
            mDisplayMatrix.reset();
            mDisplayMatrix.preRotate(-degrees);
            mDisplayMatrix.mapPoints(dims);
            dims[0] = Math.abs(dims[0]);
            dims[1] = Math.abs(dims[1]);
        }

        // Take the screenshot
        mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
        if (mScreenBitmap == null) {
            notifyScreenshotError(mContext, mNotificationManager);
            finisher.run();
            return;
        }

        if (requiresRotation) {
            // Rotate the screenshot to the current orientation
            Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,
                    mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(ss);
            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
            c.rotate(degrees);
            c.translate(-dims[0] / 2, -dims[1] / 2);
            c.drawBitmap(mScreenBitmap, 0, 0, null);
            c.setBitmap(null);
            // Recycle the previous bitmap
            mScreenBitmap.recycle();
            mScreenBitmap = ss;
        }

        // Optimizations
        mScreenBitmap.setHasAlpha(false);
        mScreenBitmap.prepareToDraw();

        // Start the post-screenshot animation
        startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
                statusBarVisible, navBarVisible);
    }

可以看到這里后兩個參數(shù):statusBarVisible奄容,navBarVisible是否可見,而這兩個參數(shù)在我們PhoneWindowManager.takeScreenshot方法傳遞的:

if (mStatusBar != null && mStatusBar.isVisibleLw())
                            msg.arg1 = 1;
                        if (mNavigationBar != null && mNavigationBar.isVisibleLw())
                            msg.arg2 = 1;

可見若果mStatusBar可見产徊,則傳遞的statusBarVisible為true昂勒,若mNavigationBar可見,則傳遞的navBarVisible為true舟铜。然后我們在截屏的時候判斷nStatusBar是否可見戈盈,mNavigationBar是否可見,若可見的時候則截屏同樣將其截屏出來谆刨。繼續(xù)回到我們的takeScreenshot方法塘娶,然后調(diào)用了:

// Take the screenshot
mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);

方法,看注釋痊夭,這里就是執(zhí)行截屏事件的具體操作了刁岸,然后我看一下SurfaceControl.screenshot方法的具體實現(xiàn),另外這里需要注意的是她我,截屏之后返回的是一個Bitmap對象虹曙,其實熟悉android繪制機(jī)制的童鞋應(yīng)該知道android中所有顯示能夠顯示的東西迫横,在內(nèi)存中表現(xiàn)都是Bitmap對象。

public static Bitmap screenshot(int width, int height) {
        // TODO: should take the display as a parameter
        IBinder displayToken = SurfaceControl.getBuiltInDisplay(
                SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN);
        return nativeScreenshot(displayToken, new Rect(), width, height, 0, 0, true,
                false, Surface.ROTATION_0);
    }

好吧酝碳,這里調(diào)用的是nativeScreenshot方法矾踱,它是一個native方法,具體的實現(xiàn)在JNI層击敌,這里就不做過多的介紹了介返。繼續(xù)回到我們的takeScreenshot方法,在調(diào)用了截屏方法screentshot之后沃斤,判斷是否截屏成功:

if (mScreenBitmap == null) {
            notifyScreenshotError(mContext, mNotificationManager);
            finisher.run();
            return;
        }

若截屏之后圣蝎,截屏的bitmap對象為空,這里判斷截屏失敗衡瓶,調(diào)用了notifyScreenshotError方法徘公,發(fā)送截屏失敗的notification通知。

static void notifyScreenshotError(Context context, NotificationManager nManager) {
        Resources r = context.getResources();

        // Clear all existing notification, compose the new notification and show it
        Notification.Builder b = new Notification.Builder(context)
            .setTicker(r.getString(R.string.screenshot_failed_title))
            .setContentTitle(r.getString(R.string.screenshot_failed_title))
            .setContentText(r.getString(R.string.screenshot_failed_text))
            .setSmallIcon(R.drawable.stat_notify_image_error)
            .setWhen(System.currentTimeMillis())
            .setVisibility(Notification.VISIBILITY_PUBLIC) // ok to show outside lockscreen
            .setCategory(Notification.CATEGORY_ERROR)
            .setAutoCancel(true)
            .setColor(context.getColor(
                        com.android.internal.R.color.system_notification_accent_color));
        Notification n =
            new Notification.BigTextStyle(b)
                .bigText(r.getString(R.string.screenshot_failed_text))
                .build();
        nManager.notify(R.id.notification_screenshot, n);
    }

然后繼續(xù)看takeScreenshot方法哮针,判斷截屏的圖像是否需要旋轉(zhuǎn)关面,若需要的話,則旋轉(zhuǎn)圖像:

if (requiresRotation) {
            // Rotate the screenshot to the current orientation
            Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,
                    mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(ss);
            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);
            c.rotate(degrees);
            c.translate(-dims[0] / 2, -dims[1] / 2);
            c.drawBitmap(mScreenBitmap, 0, 0, null);
            c.setBitmap(null);
            // Recycle the previous bitmap
            mScreenBitmap.recycle();
            mScreenBitmap = ss;
        }

在takeScreenshot方法的最后若截屏成功十厢,我們調(diào)用了:

// Start the post-screenshot animation
        startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
                statusBarVisible, navBarVisible);

開始截屏的動畫等太,好吧,看一下動畫效果的實現(xiàn):

/**
     * Starts the animation after taking the screenshot
     */
    private void startAnimation(final Runnable finisher, int w, int h, boolean statusBarVisible,
            boolean navBarVisible) {
        // Add the view for the animation
        mScreenshotView.setImageBitmap(mScreenBitmap);
        mScreenshotLayout.requestFocus();

        // Setup the animation with the screenshot just taken
        if (mScreenshotAnimation != null) {
            mScreenshotAnimation.end();
            mScreenshotAnimation.removeAllListeners();
        }

        mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
        ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation();
        ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w, h,
                statusBarVisible, navBarVisible);
        mScreenshotAnimation = new AnimatorSet();
        mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim);
        mScreenshotAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Save the screenshot once we have a bit of time now
                saveScreenshotInWorkerThread(finisher);
                mWindowManager.removeView(mScreenshotLayout);

                // Clear any references to the bitmap
                mScreenBitmap = null;
                mScreenshotView.setImageBitmap(null);
            }
        });
        mScreenshotLayout.post(new Runnable() {
            @Override
            public void run() {
                // Play the shutter sound to notify that we've taken a screenshot
                mCameraSound.play(MediaActionSound.SHUTTER_CLICK);

                mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                mScreenshotView.buildLayer();
                mScreenshotAnimation.start();
            }
        });
    }

好吧蛮放,經(jīng)過著一些列的操作之后我們實現(xiàn)了截屏之后的動畫效果了缩抡,這里暫時不分析動畫效果,我們看一下動畫效果之后做了哪些包颁?還記不記的一般情況下我們截屏之后都會收到一個截屏的notification通知瞻想?這里應(yīng)該也是在其AnimatorListenerAdapter的onAnimationEnd方法中實現(xiàn)的,也就是動畫執(zhí)行完成之后娩嚼,我們看一下其saveScreenshotInWorkerThread方法的實現(xiàn):

/**
     * Creates a new worker thread and saves the screenshot to the media store.
     */
    private void saveScreenshotInWorkerThread(Runnable finisher) {
        SaveImageInBackgroundData data = new SaveImageInBackgroundData();
        data.context = mContext;
        data.image = mScreenBitmap;
        data.iconSize = mNotificationIconSize;
        data.finisher = finisher;
        data.previewWidth = mPreviewWidth;
        data.previewheight = mPreviewHeight;
        if (mSaveInBgTask != null) {
            mSaveInBgTask.cancel(false);
        }
        mSaveInBgTask = new SaveImageInBackgroundTask(mContext, data, mNotificationManager,
                R.id.notification_screenshot).execute(data);
    }

好吧蘑险,這里主要邏輯就是構(gòu)造了一個SaveImageInBackgroundTask對象,看樣子發(fā)送截屏成功的通知應(yīng)該是在這里實現(xiàn)的岳悟,我們看一下SaveImageInBackgroundTask構(gòu)造方法的實現(xiàn)邏輯:

SaveImageInBackgroundTask(Context context, SaveImageInBackgroundData data,
            NotificationManager nManager, int nId) {
        ...

        // Show the intermediate notification
        mTickerAddSpace = !mTickerAddSpace;
        mNotificationId = nId;
        mNotificationManager = nManager;
        final long now = System.currentTimeMillis();

        mNotificationBuilder = new Notification.Builder(context)
            .setTicker(r.getString(R.string.screenshot_saving_ticker)
                    + (mTickerAddSpace ? " " : ""))
            .setContentTitle(r.getString(R.string.screenshot_saving_title))
            .setContentText(r.getString(R.string.screenshot_saving_text))
            .setSmallIcon(R.drawable.stat_notify_image)
            .setWhen(now)
            .setColor(r.getColor(com.android.internal.R.color.system_notification_accent_color));

        mNotificationStyle = new Notification.BigPictureStyle()
            .bigPicture(picture.createAshmemBitmap());
        mNotificationBuilder.setStyle(mNotificationStyle);

        // For "public" situations we want to show all the same info but
        // omit the actual screenshot image.
        mPublicNotificationBuilder = new Notification.Builder(context)
                .setContentTitle(r.getString(R.string.screenshot_saving_title))
                .setContentText(r.getString(R.string.screenshot_saving_text))
                .setSmallIcon(R.drawable.stat_notify_image)
                .setCategory(Notification.CATEGORY_PROGRESS)
                .setWhen(now)
                .setColor(r.getColor(
                        com.android.internal.R.color.system_notification_accent_color));

        mNotificationBuilder.setPublicVersion(mPublicNotificationBuilder.build());

        Notification n = mNotificationBuilder.build();
        n.flags |= Notification.FLAG_NO_CLEAR;
        mNotificationManager.notify(nId, n);

        // On the tablet, the large icon makes the notification appear as if it is clickable (and
        // on small devices, the large icon is not shown) so defer showing the large icon until
        // we compose the final post-save notification below.
        mNotificationBuilder.setLargeIcon(icon.createAshmemBitmap());
        // But we still don't set it for the expanded view, allowing the smallIcon to show here.
        mNotificationStyle.bigLargeIcon((Bitmap) null);
    }

可以發(fā)現(xiàn)在構(gòu)造方法的后面狗仔了一個NotificationBuilder對象佃迄,然后發(fā)送了一個截屏成功的Notification,這樣我們在截屏動畫之后就收到了Notification的通知了贵少。

總結(jié):

一般默認(rèn)情況下按下音量減少鍵和開機(jī)鍵會執(zhí)行截圖動作和屎,程序執(zhí)行的入口就在在PhoneWindowManager的dispatchUnhandledKey方法中;然后通過TakeScreenshotService服務(wù)執(zhí)行截圖邏輯春瞬;通過nativie方法獲取截圖的bitmap柴信,如果失敗調(diào)用失敗通知欄消息,如果成功調(diào)用截圖動畫后發(fā)送成功通知欄消息宽气。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末随常,一起剝皮案震驚了整個濱河市潜沦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绪氛,老刑警劉巖唆鸡,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異枣察,居然都是意外死亡争占,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門序目,熙熙樓的掌柜王于貴愁眉苦臉地迎上來臂痕,“玉大人,你說我怎么就攤上這事猿涨∥胀” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵叛赚,是天一觀的道長澡绩。 經(jīng)常有香客問我,道長俺附,這世上最難降的妖魔是什么肥卡? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮事镣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好蕴掏,可當(dāng)我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布藐石。 她就那樣靜靜地躺著逗嫡,像睡著了一般延窜。 火紅的嫁衣襯著肌膚如雪逆瑞。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天扫沼,我揣著相機(jī)與錄音严就,去河邊找鬼。 笑死铸董,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播汰具,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼澜倦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了稚失?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤兼蕊,失蹤者是張志新(化名)和其女友劉穎产禾,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體楞件,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡彭羹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辜梳。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖泳叠,靈堂內(nèi)的尸體忽然破棺而出作瞄,到底是詐尸還是另有隱情,我是刑警寧澤危纫,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布宗挥,位于F島的核電站,受9級特大地震影響种蝶,放射性物質(zhì)發(fā)生泄漏契耿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一螃征、第九天 我趴在偏房一處隱蔽的房頂上張望搪桂。 院中可真熱鬧,春花似錦盯滚、人聲如沸踢械。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽裸燎。三九已至,卻和暖如春泼疑,著一層夾襖步出監(jiān)牢的瞬間德绿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工退渗, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留移稳,地道東北人。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓会油,卻偏偏與公主長得像个粱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子翻翩,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,160評論 25 707
  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程都许,因...
    小菜c閱讀 6,424評論 0 17
  • 我是一尾魚 出生在陰暗的小河溝 懵懂 無知 后來有一天 我知道了海 藍(lán)色的無垠的海 活了很多年的老龜說 海里是一片...
    不二端方閱讀 234評論 0 1
  • 文 / 雅寧? 深圳出差胶征,夜宿華強(qiáng)北。窗外高樓林立桨仿,燈火閃爍睛低。 同行的安姐去到露臺,吸了兩支煙。隨后钱雷,她同我聊起了...
    浮世書童閱讀 581評論 0 0
  • “我們浪費掉了太多的青春,那是一段如此自以為是套蒂、又如此狼狽不堪的青春歲月名段,有歡笑,也有淚水泣懊;有朝氣伸辟,也有頹廢;有甜...
    五月成長筆記閱讀 183評論 0 0