2010-10-18 135 views
1

我想弄清楚整個Android授權的事情,並越來越沮喪。 在模擬器中,我運行沒有帳戶的應用程序,或者不在測試環境中的應用程序,它似乎能正常工作,返回沒有許可的響應並彈出購買應用程序現在的消息。設備上的Android授權服務器

當我嘗試在實際Android設備上運行它時,即使設備帳戶不是測試環境中的設備帳戶,也會每次都返回許可證。 此外,即使它返回許可證,「檢查許可證」框永遠不會消失,除非您單擊取消。然後它只是讓你使用該應用程序,就好像它是許可的。 這個例子大多是C & P,有一些變化。我刪除了檢查許可證按鈕和狀態文本框。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mHandler = new Handler(); 

    // Try to use more data here. ANDROID_ID is a single point of attack. 
    String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID); 

    // Library calls this when it's done. 
    mLicenseCheckerCallback = new MyLicenseCheckerCallback(); 
    // Construct the LicenseChecker with a policy. 
    mChecker = new LicenseChecker(
     this, new ServerManagedPolicy(this, 
      new AESObfuscator(SALT, getPackageName(), deviceId)), 
     BASE64_PUBLIC_KEY); 
    doCheck(); 

    ArrayAdapter<String> booksAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mBooks); 

    this.setListAdapter(booksAdapter); 
} 

protected Dialog onCreateDialog(int id) { 
    // We have only one dialog. 
    return new AlertDialog.Builder(this) 
     .setTitle(R.string.unlicensed_dialog_title) 
     .setMessage(R.string.unlicensed_dialog_body) 
     .setPositiveButton(R.string.buy_button, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
        "http://market.android.com/details?id=" + getPackageName())); 
       startActivity(marketIntent); 
       finish(); 
      } 
     }) 
     .setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }) 
     .create(); 
} 

private void doCheck() { 
    setProgressBarIndeterminateVisibility(true); 
    alertbox("status", getString(R.string.checking_license)); 
    mChecker.checkAccess(mLicenseCheckerCallback); 
} 

protected void alertbox(String title, String mymessage) 
{ 
    new AlertDialog.Builder(this) 
     .setMessage(mymessage) 
     .setTitle(title) 
     .setCancelable(true) 
     .setNeutralButton(android.R.string.cancel, 
      new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton){} 
     }) 
     .show(); 
} 

private void displayResult(final String result) { 
    mHandler.post(new Runnable() { 
     public void run() { 
      alertbox("status", result); 

      setProgressBarIndeterminateVisibility(false); 
     } 
    }); 
} 

private class MyLicenseCheckerCallback implements LicenseCheckerCallback { 
    public void allow() { 
     if (isFinishing()) { 
      // Don't update UI if Activity is finishing. 
      return; 
     } 
     // Should allow user access. 

     //displayResult(getString(R.string.allow)); 
    } 

    public void dontAllow() { 
     if (isFinishing()) { 
      // Don't update UI if Activity is finishing. 
      return; 
     } 
     //displayResult(getString(R.string.dont_allow)); 

     // Should not allow access. In most cases, the app should assume 
     // the user has access unless it encounters this. If it does, 
     // the app should inform the user of their unlicensed ways 
     // and then either shut down the app or limit the user to a 
     // restricted set of features. 
     // In this example, we show a dialog that takes the user to Market. 
     showDialog(0); 
    } 

    public void applicationError(ApplicationErrorCode errorCode) { 
     if (isFinishing()) { 
      // Don't update UI if Activity is finishing. 
      return; 
     } 
     // This is a polite way of saying the developer made a mistake 
     // while setting up or calling the license checker library. 
     // Please examine the error code and fix the error. 
     String result = String.format(getString(R.string.application_error), errorCode); 
     displayResult(result); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    mChecker.onDestroy(); 
} 

我只是不知道我需要改變,使其工作......或在許可證不知何故緩存(雖然這是我第一次在此設備上運行)和如果我可以在不擦拭設備的情況下解密它,那麼當我在其他應用上進行測試時會更好。 此外,如何刪除「檢查許可證」消息,而不必點擊取消按鈕......我應該只是讓它不顯示?

+0

問題我有:如何清除從MyLicenseCheckerCallback.allow()函數內的doCheck()中調用的alertbox()對話框。 – AndyD273 2010-10-25 16:03:27

回答

4

我剛剛進入自己的牌,所以不要把這個作爲福音,但幾件事情伸出:

或在許可證不知何故緩存(即使這是我第一次」在這個設備上運行它),如果我可以在不擦除設備的情況下解密它,那麼當我在其他應用上測試時會更好。

您正在使用ServerManagedPolicy所以審批緩存和混淆。這是推薦的方式。 (我假設提供更好的用戶體驗和更好的響應時間)爲了調試您的批准,您需要登錄到您的market profile並更改「測試響應」選項。您需要使用與您的發佈商配置文件具有相同帳戶的設備才能使測試響應適用於尚未推向市場的應用。

您的MyLicenseCheckerCallback類的allow()方法中也沒有代碼,應該可能是您清除對話框(isFinishing條件之外)的地方。

如果我能非緩存它不擦設備,因爲那將是對我做測試的其他應用程式

不錯

基於LicenseValidator.java看起來批准存儲在prefs在私人模式下com.android.vending.licensing.ServerManagedPolicy文件。您可以使用sharedpreferences editor從應用程序的其他位置清除它。

再一次,我不是專業人士,所以我可能是錯的,但我認爲你可能能夠解決你的錯誤,如果你得到它配置正確。

+0

感謝您的回覆。我對此也很新,對一般的java/android編程也很新穎。示例應用程序中是否有MyLicenseCheckerCallback示例?我沒有看到它,以及我正在使用的所有C&P。我會再看一遍,看看我能找到什麼。 – AndyD273 2010-10-21 15:15:08