Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

targetSdkVersion 28, and REQUEST_INSTALL_PACKAGES permission always return false #281

Open
Jack-Xiao opened this issue Jan 11, 2019 · 4 comments

Comments

@Jack-Xiao
Copy link

  1. targetSdkVersion 28
    packageManager.canRequestPackageInstalls() return true when i setting install permission, but rxPermissions?.request(Manifest.permission.REQUEST_INSTALL_PACKAGES) always return false。How to relsolve

  2. the test project:
    https://github.com/Jack-Xiao/RxPermissionsTest

@qqnp1100
Copy link

I wrote a simple extension for RxPermissions.

import android.annotation.TargetApi
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject

fun RxPermissions.requestPackageInstalls(activity: FragmentActivity): Observable<Boolean> {
    var fragment: PackageInstallsFragment? = null
    if (activity != null && !activity.isFinishing) {
        val fm = activity.supportFragmentManager
        fragment = fm.findFragmentByTag(PackageInstallsFragment.TAG) as PackageInstallsFragment?
        if (fragment == null) {
            fragment = PackageInstallsFragment()
            fm.beginTransaction()
                .add(fragment, PackageInstallsFragment.TAG)
                .commitNow()
        }
        var isGranted = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            activity.packageManager.canRequestPackageInstalls()
        } else {
            true
        }
        if (isGranted) {
            return Observable.just(true)
        } else {
            if (!fragment.isRequest) {
                fragment.sub = PublishSubject.create()
            }
            fragment.requestPermissions()
        }
    }
    return fragment!!.sub!!
}

class PackageInstallsFragment : Fragment() {
    var sub: PublishSubject<Boolean>? = null
    var isRequest = false

    companion object {
        const val TAG = "PackageInstallsFragment"
        const val CODE = 10086
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
    }

    fun requestPermissions() {
        isRequest = true
        val uri = Uri.fromParts("package", activity!!.packageName, null)
        startActivityForResult(Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, uri), CODE)
    }

    @TargetApi(Build.VERSION_CODES.O)
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        isRequest = false
        sub!!.onNext(activity!!.packageManager.canRequestPackageInstalls())
        sub!!.onComplete()
    }
}

use

val rxPermissions = RxPermissions(this)
rxPermissions.requestPackageInstalls(this).subscribe {
                if (it) {

                } else {

                }
            }

ezgif-3-5ea05e68ded3

@Jack-Xiao
Copy link
Author

Jack-Xiao commented Feb 21, 2019

@qqnp1100 thank you, i understand your means, and use getPackageManager().canRequestPackageInstalls() can resolve it。
However i means why rxPermissions?.request(Manifest.permission.REQUEST_INSTALL_PACKAGES) always return false when turn on the switch。

@yungtat
Copy link

yungtat commented May 16, 2019

是继承RxPermission写的吗?

@consp1racy
Copy link

Request_install_packages is not a dangerous/runtime permission. You can't use RxPermissions with it.

It's a normal permission. You declare it in manifest and then you can use PackageInstaller API to install packages interactively.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants