codelab-android-workmanager icon indicating copy to clipboard operation
codelab-android-workmanager copied to clipboard

The `resolveActivity` doesn't work for Android 11 like this

Open TinaT2 opened this issue 3 years ago • 4 comments

The lint says It needs @SuppressLint("QueryPermissionsNeeded"). Also, it doesn't open the activity. Code lab page number: https://developer.android.com/codelabs/android-workmanager#8

override fun onCreate(savedInstanceState: Bundle?) {
   // Setup view output image file button
   binding.seeFileButton.setOnClickListener {
       viewModel.outputUri?.let { currentUri ->
           val actionView = Intent(Intent.ACTION_VIEW, currentUri)
           actionView.resolveActivity(packageManager)?.run {
               startActivity(actionView)
           }
       }
   }
}

TinaT2 avatar Jan 10 '22 11:01 TinaT2

Reviewing the final code, i see a missing <query> tag in AndroidManifest.xml. Maybe this is the solution and, after add this code, the @SuppressLint("QueryPermissionsNeeded") is not suggested anymore.

<queries>
    <package android:name="com.example.background" />
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:mimeType="image/png"/>
    </intent>
</queries>

AlveZs avatar Feb 28 '22 16:02 AlveZs

Thanks! This was exactly my issue on my Android 12 Device. I added @AlveZs's <queries> code in the AndroidManifest.xml right under the <uses-permission> section and it worked.

tamarabuilds avatar Apr 25 '22 04:04 tamarabuilds

+1 @AlveZs solution works.

shalperin avatar May 21 '22 16:05 shalperin

Since we're running the default handler, a simple startActivity(Intent(Intent.ACTION_VIEW, currentUri)) call would work without a need for resolution and hence, a need for the queries permission.

Sp3EdeR avatar Sep 01 '22 13:09 Sp3EdeR