首先准备2个包名和1个地址:
- Google身份验证器包名:
com.google.android.apps.authenticator2
- GooglePlay Store 包名:
com.android.vending
- Google身份验证器在GooglePlay Store中的应用详情页面URL:
https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2
原理就不说了,很简单,就是根据包名打开目标App。
代码如下(Kotlin):
val intent = context.packageManager.getLaunchIntentForPackage(GOOGLE_AUTH2_PACKAGE_NAME)
context.startActivity(intent)
如果没有安装Google身份验证器,就打开GooglePlay Store应用详情页去下载,如果没有安装GooglePlay,则打开网页GooglePlay Store应用详情页下载。
代码如下(Kotlin):
//打开GooglePlay Store Google身份验证器应用详情页
val uri = Uri.parse("market://details?id=$GOOGLE_AUTH2_PACKAGE_NAME")
val intent = Intent(Intent.ACTION_VIEW, uri)
//这里指定PackageName为GooglePlay Store,因为其他应用商店也实现了`market://`协议。这里指定GooglePlay Store来处理这个Intent。
intent.setPackage(GOOGLE_PLAY_PACKAGE_NAME)
//新建一个Activity任务栈
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//判断实际是否可以处理这个Intent,如果不能处理,实际中是没有安装GooglePlay Store。
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
} else {
//没有安装跳转到浏览器
goWebGooglePlayStore()
}
//打开浏览器
private fun goWebGooglePlayStore() {
val uri = Uri.parse(GOOGLE_AUTH2_STORE_URL)
val intent = Intent(Intent.ACTION_VIEW)
intent.data = uri
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
} else {
//什么?没有浏览器?可以考虑换个手机了 机型友情提示下
}
}
完整代码如下(Kotlin):
package com.android.demo.googleauth2
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.text.TextUtils
import android.widget.Toast
const val GOOGLE_AUTH2_PACKAGE_NAME = "com.google.android.apps.authenticator2"
const val GOOGLE_PLAY_PACKAGE_NAME = "com.android.vending"
const val GOOGLE_AUTH2_STORE_URL = "https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2"
fun Context.openGoogleAuth2() {
//是否安装了Google 身份验证器
if (isInstalledApp(GOOGLE_AUTH2_PACKAGE_NAME)) {
//打开Google身份验证器
startActivity(packageManager.getLaunchIntentForPackage(GOOGLE_AUTH2_PACKAGE_NAME))
} else {
//是否安装了GooglePlay Store
if (isInstalledApp(GOOGLE_PLAY_PACKAGE_NAME)) {
//打开GooglePlay Store Google身份验证器应用详情页
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$GOOGLE_AUTH2_PACKAGE_NAME"))
intent.setPackage(GOOGLE_PLAY_PACKAGE_NAME)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//再次判断实际中是否已安装了GooglePlay Store
intent.resolveActivity(packageManager)?.let {
startActivity(intent)
} ?:
//没有安装跳转到浏览器
goWebGooglePlayStore(this)
} else {
//没有安装跳转到浏览器
goWebGooglePlayStore(this)
}
}
}
fun Context.isInstalledApp(packageName: String): Boolean {
val packageManager: PackageManager = packageManager
// 获取所有已安装程序的包信息
val packageInfo = packageManager.getInstalledPackages(0)
for (info in packageInfo) {
if (TextUtils.equals(packageName, info.packageName)) {
return true
}
}
return false
}
private fun goWebGooglePlayStore(context: Context) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(GOOGLE_AUTH2_STORE_URL)
intent.resolveActivity(context.packageManager)?.let {
context.startActivity(intent)
} ?: Toast.makeText(context, "No browser installed", Toast.LENGTH_SHORT).show()
}
-----完-----
。。撒花。。
网友评论