调用startActivityForResult后未调用onActivityResult

时间:2019-03-24 16:49:26

标签: android kotlin

我有一个活动,该人的功能是拍摄当前屏幕的屏幕截图。这是该活动的onCreate方法的样子:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(null)

    mProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
    startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE)
}

然后在它下面,我有如下所示的onActivityResult方法:

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == REQUEST_CODE) {
        sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data)

        val externalFilesDir = getExternalFilesDir(null)
        if (externalFilesDir != null) {
            STORE_DIRECTORY = externalFilesDir.absolutePath + "/screenshots/"
            val storeDirectory = File(STORE_DIRECTORY)
            if (!storeDirectory.exists()) {
                val success = storeDirectory.mkdirs()
                if (!success) {
                    Log.e("TAGE", "failed to create file storage directory.")
                    return
                }
            }
        } else {
            Log.e("TAG", "failed to create file storage directory, getExternalFilesDir is null.")
            return
        }

        // display metrics
        val metrics = resources.displayMetrics
        mDensity = metrics.densityDpi
        mDisplay = windowManager.defaultDisplay

        // create virtual display depending on device width / height
        createVirtualDisplay()

        // register orientation change callback
        mOrientationChangeCallback = OrientationChangeCallback(this)
        if (mOrientationChangeCallback.canDetectOrientation()) {
            mOrientationChangeCallback.enable()
        }

        // register media projection stop callback
        sMediaProjection.registerCallback(MediaProjectionStopCallback(), mHandler)
    }
}

活动在清单中是这样的:

        <activity android:name=".ScreenshotActivity"
              android:theme="@android:style/Theme.NoDisplay"
              android:excludeFromRecents="true"
              android:screenOrientation="portrait">
    </activity>

我的问题是,从未调用过onActivityResult函数,我曾尝试在该函数中添加断点,但它们从未被命中。

0 个答案:

没有答案