安装未知来源
需求简介
当App需要安装未知来源的应用的时候,需要开启"安装位置来源应用”的权限。
实现方案
//跳转到对应的设置页面进行权限操作
private fun gotoSettingsPage() {
val packageURI = Uri.parse("package:"+ activity.packageName)
val intent: Intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI)
startActivityForResult(intent, WPermissionFragment.INSTALL_PERMISSION)
}
//在onActivityResult时进行判断是否已经开启
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(INSTALL_PERMISSION == requestCode){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val haveInstallPermission: Boolean = GlobalConfig.getInstance().context.packageManager.canRequestPackageInstalls()
if (haveInstallPermission){
} else{
}
}
}
}
存在问题及解决方案
因为是异步下载,无法确定需要哪个页面去发起跳转。
解决方案:
拿到当前的activity,将fragment注册进activity中,在Fragment中处理相关内容。
代码实现
将请求权限封装成了RxJava形式,响应式请求:
class RxDownloadPermission(val activity: FragmentActivity) {
private val fragmentManager = activity.supportFragmentManager
companion object{
const val TAG :String= "InstallPermission"
}
var permissionFragment: WPermissionFragment? = null
fun requestInstallPermission(): Subject<Boolean> {
///1、判断是否有承载的Fragment,有则直接放到manager中,没有重新创建并放到manager中
///2、启动对应的Activity
///3、监听onActivityResult
permissionFragment = fragmentManager.findFragmentByTag(TAG) as WPermissionFragment?
if(permissionFragment == null){
permissionFragment = WPermissionFragment().also {
fragmentManager.beginTransaction().add(it, TAG).commitNow()
}
}
val subject: Subject<Boolean> = PublishSubject.create()
permissionFragment?.subject = subject
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
gotoSettingsPage()
} else{
subject.onNext(true)
subject.onComplete()
}
return subject
}
@RequiresApi(Build.VERSION_CODES.O)
private fun gotoSettingsPage() {
val packageURI = Uri.parse("package:"+ activity.packageName)
val intent: Intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI)
permissionFragment?.startActivityForResult(intent, WPermissionFragment.INSTALL_PERMISSION)
}
}
对应Fragment:
class WPermissionFragment : Fragment() {
var subject :Subject<Boolean>? = null
companion object{
const val INSTALL_PERMISSION: Int = 1
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(INSTALL_PERMISSION == requestCode){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val haveInstallPermission: Boolean = GlobalConfig.getInstance().context.packageManager.canRequestPackageInstalls()
if (haveInstallPermission){
subject?.onNext(true)
} else{
subject?.onNext(false)
}
}
subject?.onComplete()
}
}
}
TIPS:
如果只是升级包安装,可以直接安装apk:
fun installApk(context: Context, downloadApk: String): Unit {
val intent = Intent(Intent.ACTION_VIEW)
val file = File(downloadApk)
LOGGER.i("安装路径==$downloadApk")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val apkUri: Uri = FileProvider.getUriForFile(context, context.applicationInfo.packageName + ".fileProvider", file)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
} else {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val uri: Uri = Uri.fromFile(file)
intent.setDataAndType(uri, "application/vnd.android.package-archive")
}
context.startActivity(intent)
}