startActivityForResult,intent: Intent { cmp=com.android.mms/.ui.ConversationList (has extras) },
时间: 2024-06-05 12:11:59 浏览: 20
这是一个启动 Android 短信应用的代码,使用 startActivityForResult 方法启动一个名为 "com.android.mms.ui.ConversationList" 的 Activity,并传递了一些额外的参数。当该 Activity 返回结果时,将调用 onActivityResult 方法以处理结果。具体实现和结果处理需要根据具体情况进行编写。
相关问题
'startActivityForResult(android.content.Intent, int)' is deprecated
`startActivityForResult(Intent, int)` is indeed deprecated since Android 11 (API level 30). It means that this method will no longer be supported in future versions of Android. Instead, you should use the `registerForActivityResult` method, which is a more modern way to handle activities that return results.
Here's an example of how to use `registerForActivityResult`:
```
val someActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// Handle the result
val data: Intent? = result.data
}
}
// Call the activity with startActivityForResult
val intent = Intent(this, SomeActivity::class.java)
someActivityResultLauncher.launch(intent)
```
With this approach, you create an instance of `ActivityResultLauncher` by calling `registerForActivityResult`, passing in an instance of `ActivityResultContracts.StartActivityForResult`. You then define a lambda that will be called when the activity returns a result. Finally, you call `someActivityResultLauncher.launch(intent)` to start the activity and wait for the result to come back.
Note that `registerForActivityResult` is only available in AndroidX libraries, so if you're not already using them, you'll need to migrate your project to AndroidX before you can use this method.
如何解决FATAL EXCEPTION: main Process: com.example.administrator.bookmanager, PID: 18790 java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{ba36b24 18790:com.example.administrator.bookmanager/u0a131} (pid=18790, uid=10131) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
这个错误表明你的应用程序没有权限打开媒体文档提供程序。要解决这个问题,你需要在你的应用程序中请求权限并使用 `ACTION_OPEN_DOCUMENT` 或相关的 API 打开媒体文档提供程序。你可以按照以下步骤解决这个问题:
1. 在你的 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
这将允许你的应用程序读取外部存储器的内容。
2. 在你的代码中请求这个权限:
```java
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
```
这会弹出一个对话框,请求用户允许你的应用程序读取外部存储器的内容。
3. 当用户允许你的应用程序访问外部存储器时,你可以使用 `ACTION_OPEN_DOCUMENT` 或相关的 API 打开媒体文档提供程序。
```java
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE);
```
这将打开媒体文档提供程序,用户可以选择他们所需的文档。
请注意,如果你使用的是 Android 10 或更高版本,你需要使用 `ACTION_OPEN_DOCUMENT_TREE` 或相关的 API 来访问外部存储器。