1. 程式人生 > >Android從app跳轉到google Play市場

Android從app跳轉到google Play市場

有時候app開發中會有從應用內跳轉到Google play做一些業務的邏輯操作,比如引導使用者對應用做好評的操作,這裡放段很管用的程式碼來實現這個需求:


            // 做跳轉到谷歌play做好評的業務邏輯
            //這裡開始執行一個應用市場跳轉邏輯,預設this為Context上下文物件
            var intent: Intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("market://details?id=" + "com.blossom.ripple") //跳轉到應用市場,非Google Play市場一般情況也實現了這個介面
            //存在手機裡沒安裝應用市場的情況,跳轉會包異常,做一個接收判斷
            if (intent.resolveActivity(activity!!.packageManager) != null) { //可以接收
                startActivity(intent)
            } else { //沒有應用市場,我們通過瀏覽器跳轉到Google Play
                intent.data = Uri.parse("https://play.google.com/store/apps/details?id=" + "com.blossom.ripple");
                //這裡存在一個極端情況就是有些使用者瀏覽器也沒有,再判斷一次
                if (intent.resolveActivity(activity!!.packageManager) != null) { //有瀏覽器
                    startActivity(intent);
                } else { //天哪,這還是智慧手機嗎?
                    Toast.makeText(activity, "You don't have an app market installed, not even a browser!", Toast.LENGTH_SHORT).show()
                }
            }