首页 > 解决方案 > 将活动结果用于多种用途

问题描述

我需要做两件事:-

  1. 一种从存储中选择文件
  2. 其次是请求发送短信的权限。

为此,我需要使用活动结果和活动结果合同。(这是新方法)

如何注册两个不同的合约?(选择文件并请求权限)

1.请求权限:

resultLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()
        ) {
                isGranted: Boolean ->
            if (isGranted) {
                // Permission is granted. Continue the action or workflow in your
                // app.
                sendSMS()
            } else {
                // Explain to the user that the feature is unavailable because the
                // features requires a permission that the user has denied. At the
                // same time, respect the user's decision. Don't link to system
                // settings in an effort to convince the user to change their
                // decision.
                Toast.makeText(requireContext(), "sms text can't be delivered", Toast.LENGTH_LONG).show()
            }
        }

2.从存储中读取文件:

resultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { result : Any? ->
            Log.d(TAG, "resultLauncher execute")
            if (result == null)
            {
                //Toast.makeText(context, getString(R.string.quiz_resume_error_message), Toast.LENGTH_SHORT).show()
                Log.d(TAG, "result is null")
            } else if (result is Uri) {
                    Log.d(TAG, "result is Uri type")
                user.resume = File(result.toString())
                    fab.visibility = View.VISIBLE
                    /*if (user.resume != null)
                        Toast.makeText(context, getString(R.string.quiz_resume_message), Toast.LENGTH_SHORT).show()*/
                }

我怎样才能把这些放在一起?

谢谢!

标签: androidkotlinregisterforactivityresult

解决方案


推荐阅读