2012-06-22 81 views
9

我的應用程序已準備好提交,但最近我已經瞭解了許可。安卓許可教程 - 最後一步

我已經找到了一步一步的教程在線:http://twistbyte.com/tutorial/using-the-android-licensing-service-step-by-step

我已經進口許可庫到Eclipse和本教程中介紹創建LicenseCheckActivity類。

我正在教程的最後一步,第7點。教程說我的班級應該延長LicenseCheckActivity。但是,我希望檢查許可的班級已經擴展了活動。

如何使用我的LicenseCheckActivity類中的checkLicense()方法?

這裏是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    // Check the license 
    LicenseCheckActivity l = new LicenseCheckActivity(); 
    checkLicense(); 

這給了我下面的錯誤:

Cannot instantiate the type LicenseCheckActivity

這裏是我LicenseCheckActivity

public abstract class LicenseCheckActivity extends Activity { 

static boolean licensed = true; 
static boolean didCheck = false; 
static boolean checkingLicense = false; 
static final String BASE64_PUBLIC_KEY = "MY BASE KEY"; 

LicenseCheckerCallback mLicenseCheckerCallback; 
LicenseChecker mChecker; 

Handler mHandler; 

SharedPreferences prefs; 

// REPLACE WITH YOUR OWN SALT , THIS IS FROM EXAMPLE 
private static final byte[] SALT = new byte[] { -46, 65, 30, -128, -103, 
     -57, 74, -64, 51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64, 
     89 }; 

private void displayResult(final String result) { 
    mHandler.post(new Runnable() { 
     public void run() { 

      setProgressBarIndeterminateVisibility(false); 

     } 
    }); 
} 

protected void doCheck() { 

    didCheck = false; 
    checkingLicense = true; 
    setProgressBarIndeterminateVisibility(true); 

    mChecker.checkAccess(mLicenseCheckerCallback); 
} 

protected void checkLicense() { 

    Log.i("LICENSE", "checkLicense"); 
    mHandler = new Handler(); 

    // Try to use more data here. ANDROID_ID is a single point of attack. 
    String deviceId = Settings.Secure.getString(getContentResolver(), 
      Settings.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); 

    // mChecker = new LicenseChecker(
    // this, new StrictPolicy(), 
    // BASE64_PUBLIC_KEY); 

    doCheck(); 
} 

protected class MyLicenseCheckerCallback implements LicenseCheckerCallback { 

    public void allow() { 
     Log.i("LICENSE", "allow"); 
     if (isFinishing()) { 
      // Don't update UI if Activity is finishing. 
      return; 
     } 
     // Should allow user access. 
     displayResult(getString(R.string.allow)); 
     licensed = true; 
     checkingLicense = false; 
     didCheck = true; 

    } 

    public void dontAllow() { 
     Log.i("LICENSE", "dontAllow"); 
     if (isFinishing()) { 
      // Don't update UI if Activity is finishing. 
      return; 
     } 
     displayResult(getString(R.string.dont_allow)); 
     licensed = false; 
     // 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. 
     checkingLicense = false; 
     didCheck = true; 

     showDialog(0); 
    } 

    public void applicationError(int errorCode) { 
     Log.i("LICENSE", "error: " + errorCode); 
     if (isFinishing()) { 
      // Don't update UI if Activity is finishing. 
      return; 
     } 
     licensed = false; 
     // 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); 
     checkingLicense = false; 
     didCheck = true; 

     displayResult(result); 
     // showDialog(0); 
    } 

    public void allow(int reason) { 
     // TODO Auto-generated method stub 

    } 

    public void dontAllow(int reason) { 
     // TODO Auto-generated method stub 

    } 

} 

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(); 
         } 
        }) 

      .setCancelable(false) 
      .setOnKeyListener(new DialogInterface.OnKeyListener() { 
       public boolean onKey(DialogInterface dialogInterface, 
         int i, KeyEvent keyEvent) { 
        Log.i("License", "Key Listener"); 
        finish(); 
        return true; 
       } 
      }).create(); 

} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (mChecker != null) { 
     Log.i("LIcense", "distroy checker"); 
     mChecker.onDestroy(); 
    } 
} 

} 
+0

LicenceCheckActivity似乎是靜態的。也許嘗試'LicenseCheckActivity.checkLicense();'。 – Mualig

+0

@Mualig我已經嘗試了你的代碼,並且出現以下錯誤:無法對類型LicenseCheckActivity中的非靜態方法checkLicense()進行靜態引用。 Eclipse提供了一個快速修復:將CheckLicense()的修飾符更改爲'static'。但checklicense中使用的AESObfuscator不接受靜態引用。 – tiptopjat

回答

5

因爲你是你得到的錯誤試圖

// Check the license 
LicenseCheckActivity l = new LicenseCheckActivity(); 

實例活動。你從不這樣做!始終使用ActivityManager和Intents開始活動並在它們之間傳遞信息。

解決辦法:

既然你想讓你的啓動類繼續擴大活動,不能把它擴展LicenseCheckActivity唯一的其他建議是在你啓動類移動代碼。

例:

採取所有的代碼從LicenseCheckActivity並將其移動到您的MainActivity類,然後你可以調用checkLicense()MainActivity

3

教程狀態的onCreate方法您ActivityLicenseCheckActivity延長。

例子:

class ExampleActivity extends LicenseCheckActivity{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.main); 

     Toast.makeText(this, "Checking Application License", Toast.LENGTH_SHORT).show(); 
     // Check the license 
     checkLicense(); 
    } 
} 

因爲LicenseCheckActivity擴展Activity,你ExampleActivity類仍然會繼承您的Activity方法,如果你有LicenseCheckActivity擴展它。

如果您需要您的ExampleActivity類延伸ListActivity,那麼您可以讓LicenseCheckActivity擴展ListActivity而不是活動。

+0

我正在嘗試相同的代碼,但每次應用程序啓動它只顯示我的吐司消息「許可證檢查驗證」,並正常啓動。它沒有顯示我購買項目的對話,因爲我沒有上傳它在谷歌播放。 – user2592807