Android App通过应用宝获取版本信息,从而跳转到不同应用商店更新APP

1、跳转主流应用市场工具方法

const val YYB = "com.tencent.android.qqdownloader"
const val HUAWEI = "com.huawei.appmarket"
const val XIAOMI = "com.xiaomi.market"
const val OPPO = "com.oppo.market"
const val VIVO = "com.bbk.appstore"
const val SANSUNG="com.sec.android.app.samsungapps"

fun isAppAvailable(packageName: String): Boolean {
    // 获取packagemanager
    val packageManager: PackageManager = App.instance.packageManager
    // 获取所有已安装程序的包信息
    val pinfo: List = packageManager.getInstalledPackages(0)
    // 用于存储所有已安装程序的包名
    val pName: MutableList = ArrayList()
    // 从pinfo中将包名字取出
    for (i in pinfo.indices) {
        val pf: String = pinfo[i].packageName
        pName.add(pf)
    }
    // 判断pName中是否有目标程序的包名,有true,没有false
    return pName.contains(packageName)
}

/**
 * 启动到应用商店app详情界面
 *
 * @param appPkg    目标App的包名
 * @param marketPkg 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面
 */
fun launchAppDetail(marketPkg: String?) {
    try {
        val uri: Uri = Uri.parse("market://details?id=com.tencent.mobileqq")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        if (!TextUtils.isEmpty(marketPkg)) {
            intent.setPackage(marketPkg)
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        App.instance.startActivity(intent)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

2、通过抓取网页版本信息,对比本地,然后更新app

class UpdateAsyncTask(var context: Context) : AsyncTask() {

    companion object {
        private const val tencentUrl: String = "https://android.myapp.com/myapp/detail.htm?apkName=cn.com.blackview.dashcam"
        private const val getElementsByClass: String = "pp-comp-extra-p"
    }

    override fun doInBackground(vararg params: String): String {
        var version = ""
        kotlin.runCatching {
            val doc = Jsoup.connect(tencentUrl).get()
            val elementsByClass = doc.getElementsByClass(getElementsByClass)
            if (elementsByClass.size > 1) {
                version = elementsByClass[1].text()
            }
        }.getOrNull()
        return version
        //通过本地接口
//        var result = ""
//        kotlin.runCatching {
//            val url = URL("http://test.blackview4g.com:8910/community/api/appVersion")
//            val urlConnection = url.openConnection() as HttpURLConnection
//            val responseCode: Int = urlConnection.responseCode
//            if (responseCode == HttpURLConnection.HTTP_OK) {
//                result = readStream(urlConnection.inputStream)
//            }
//        }.getOrNull()
//
//        return result

    }

    override fun onPostExecute(s: String) {
        super.onPostExecute(s)
        //版本:4.5.1421115
        kotlin.runCatching {
            val versionCode = s.split(":")[1].split(".")[2].toInt()
            val localVersionCode = App.instance.packageManager.getPackageInfo(App.instance.packageName, 0)
                .versionName.toString().split(".")[2].toInt()
            Log.e("dd", "localVersionCode:$localVersionCode")
            Log.e("dd", "versionCode:$versionCode")
            if (localVersionCode < versionCode) {
                if (context is BaseActivity) {
                    if ((context as BaseActivity).isFinishing) {
                        return
                    }
                }
                LDDialog.Builder(context)
                    .setTitle(App.instance.resources.getString(R.string.album_note))
                    .setContent(App.instance.resources.getString(R.string.album_note_upgrade_title))
                    .setRightButton(App.instance.resources.getString(R.string.album_confirm))
                    { dialog: IDialog ->
                        dialog.dismiss()
                        setIntent()
                    }
                    .setLeftButton(App.instance.resources.getString(R.string.album_cancel))
                    { dialog: IDialog -> dialog.dismiss() }
                    .show()
            }
        }.getOrNull()
    }

    private fun setIntent() {
        when {
            isAppAvailable(YYB) -> {
                launchAppDetail(YYB)
            }
            isAppAvailable(HUAWEI) -> {
                launchAppDetail(HUAWEI)
            }
            isAppAvailable(XIAOMI) -> {
                launchAppDetail(XIAOMI)
            }
            isAppAvailable(OPPO) -> {
                launchAppDetail(OPPO)
            }
            isAppAvailable(VIVO) -> {
                launchAppDetail(VIVO)
            }
            else -> {
                val uri = Uri.parse(tencentUrl)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                App.instance.startActivity(intent)
            }
        }
    }

    private fun readStream(inputStream: InputStream): String {
        var reader: BufferedReader? = null
        val response = StringBuffer()
        try {
            reader = BufferedReader(InputStreamReader(inputStream))
            var line: String?
            while (reader.readLine().also { line = it } != null) {
                response.append(line)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            if (reader != null) {
                try {
                    reader.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return response.toString()
    }

}

你可能感兴趣的:(移动开发,android,App,App更新,Kotlin)