Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 Android-Rate 快速做出「去應用市場給好評」的引導彈窗

編輯:關於 Android 程式設計

最近寫產品的文章少了些。家裡的小寶寶(麥麥)正處於愛咬人的階段,得花心思照顧;加上最近沉迷魔獸世界,寫作的時間就被擠掉了。今天偶然看到一個在 App 內提示使用者跳轉應用市場評分的開源庫,覺得做得挺完整,推薦給大家。


先看執行效果:

使用者點進去後,它會詢問是否現在評分、稍後再說等等。原作者把這一整套流程封裝得很乾淨,接入成本很低。我們先看看一個最簡單的提示頁要怎麼設定。

在 Activity 的初始化階段把各項參數設定好:

AppRate.with(this) .setStoreType(StoreType.GOOGLEPLAY) //默認Google, 還有個是亞馬遜 .setInstallDays(0) // 默認10,這裡為了演示效果,設置裝的時候就演示 .setLaunchTimes(3) // 設置啟動的時間 .setRemindInterval(2) // 提醒的間隔 .setShowLaterButton(true) // 是否顯示推遲按鈕 .setDebug(false) // 是否Debug狀態,默認false .setMessage("分享我們好嗎")//設置說明文字 .setOnClickButtonListener(new OnClickButtonListener() { //按鈕監聽事件 @Override public void onClickButton(int which) { Log.d(MainActivity.class.getName(), Integer.toString(which)); } }) .monitor();

等到需要提示的時機,把它顯示出來即可:

// 顯示對話框 AppRate.showRateDialogIfMeetsConditions(this);

標題、按鈕文字之類的也都能改:

setTitle()//設置標題 setNeutralText()//設置按鈕文字內容 

如果你嫌預設 UI 不合公司美工的規範,也可以整包換掉,呼叫 setView 塞入自訂版面:

LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root)); AppRate.with(this).setView(view).monitor();

以上是基礎設定。接下來才是比較麻煩的部分:作者顯然不是國內開發者,沒考慮到國內應用市場林立的現實,而我們又用不上 Google Play,所以預設行為必須改寫——這也是我先前不貼下載位置的原因。

看一下原始碼結構:

負責分享 URL 與跳轉的是這幾個類別:

UriHelper、IntentHelper、StoreType、DialogManager。

StoreType 是列舉型別,用來區分要跳哪個市場。要支援小米、豌豆莢,就在裡面補上:

public enum StoreType { GOOGLEPLAY, AMAZON//, 你要的內容 }

IntentHelper 負責組出實際的 Intent:

final class IntentHelper { private static final String GOOGLE_PLAY_PACKAGE_NAME = "com.android.vending"; private IntentHelper() { } static Intent createIntentForGooglePlay(Context context) { String packageName = context.getPackageName(); Intent intent = new Intent(Intent.ACTION_VIEW, getGooglePlay(packageName)); if (isPackageExists(context, GOOGLE_PLAY_PACKAGE_NAME)) { intent.setPackage(GOOGLE_PLAY_PACKAGE_NAME); } return intent; } static Intent createIntentForAmazonAppstore(Context context) { String packageName = context.getPackageName(); return new Intent(Intent.ACTION_VIEW, getAmazonAppstore(packageName)); } }

UriHelper 負責 URL 拼接:

final class UriHelper { private static final String GOOGLE_PLAY = "https://play.google.com/store/apps/details?id="; private static final String AMAZON_APPSTORE = "amzn://apps/android?p="; private UriHelper() { } static Uri getGooglePlay(String packageName) { return packageName == null ? null : Uri.parse(GOOGLE_PLAY + packageName); } static Uri getAmazonAppstore(String packageName) { return packageName == null ? null : Uri.parse(AMAZON_APPSTORE + packageName); } static boolean isPackageExists(Context context, String targetPackage) { PackageManager pm = context.getPackageManager(); List packages = pm.getInstalledApplications(0); for (ApplicationInfo packageInfo : packages) { if (packageInfo.packageName.equals(targetPackage)) return true; } return false; } }

這些都改完之後,只要在 DialogManager 的點擊回呼裡加上判斷:

builder.setPositiveButton(options.getPositiveText(context), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { final Intent intentToAppstore = options.getStoreType() == StoreType.GOOGLEPLAY ? createIntentForGooglePlay(context) : createIntentForAmazonAppstore(context); context.startActivity(intentToAppstore); setAgreeShowDialog(context, false); if (listener != null) listener.onClickButton(which); } });

原本點擊評分後只會判斷是 Google Play 還是 Amazon,你把國內市場的判斷加進 onClick 裡就好了。

這個元件已收錄進我的 Android 開源大套餐:https://github.com/ddwhan0123/Useful-Open-Source-Android/blob/master/README.md

專案網址:https://github.com/hotchemi/Android-Rate

下載地址:https://github.com/hotchemi/Android-Rate/archive/master.zip

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved