2012-06-22 30 views
9

Ứng dụng của tôi đã sẵn sàng để gửi ngay bây giờ nhưng gần đây tôi đã biết về Cấp phép.Hướng dẫn cấp phép Android - Bước cuối cùng

Tôi đã tìm thấy một từng bước bằng cách hướng dẫn trực tuyến: http://twistbyte.com/tutorial/using-the-android-licensing-service-step-by-step

tôi đã nhập viện cấp phép vào Eclipse và tạo lớp LicenseCheckActivity như mô tả trong hướng dẫn.

Tôi đang ở bước cuối cùng của hướng dẫn, số điểm 7. Hướng dẫn cho biết lớp học của tôi nên mở rộng LicenseCheckActivity. Tuy nhiên, lớp mà tôi muốn kiểm tra giấy phép đã mở rộng Hoạt động.

Tôi làm cách nào để sử dụng phương pháp checkLicense() từ lớp học LicenseCheckActivity của mình?

Đây là mã của tôi:

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

này mang lại cho tôi những lỗi sau:

Cannot instantiate the type LicenseCheckActivity

Đây là lớp LicenseCheckActivity tôi

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' có vẻ là' tĩnh'. Có thể thử với 'LicenseCheckActivity.checkLicense();'. – Mualig

+0

@Mualig Tôi đã thử mã của bạn và tôi nhận được lỗi sau: Không thể thực hiện tham chiếu tĩnh đến phương thức không tĩnh checkLicense() từ loại LicenseCheckActivity. Eclipse cung cấp bản sửa lỗi nhanh: Thay đổi công cụ sửa đổi của CheckLicense() thành 'tĩnh'.Nhưng AESObfuscator được sử dụng trong checklicense không chấp nhận tham chiếu tĩnh. – tiptopjat

Trả lời

5

Bạn đang nhận lỗi rằng vì bạn đang cố gắng

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

Ví dụ về hoạt động. Bạn không bao giờ làm điều này! Luôn sử dụng ActivityManager và Intents để bắt đầu các hoạt động và truyền thông tin giữa chúng.

Giải pháp:

Vì bạn muốn lớp bắt đầu của bạn để tiếp tục mở rộng hoạt động và không thể có nó mở rộng LicenseCheckActivity chỉ đề nghị khác sẽ được di chuyển mã trong lớp bắt đầu của bạn.

Ex:

Mang tất cả các mã từ LicenseCheckActivity của bạn và di chuyển nó vào lớp MainActivity bạn và sau đó bạn có thể gọi checkLicense() trong phương thức onCreate của MainActivity

3

Các trạng thái hướng dẫn của bạn để mở rộng của bạn Activity với LicenseCheckActivity .

Ví dụ:

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 kéo dài Activity, lớp học của bạn ExampleActivity vẫn sẽ kế thừa Activity phương pháp của bạn nếu bạn mở rộng nó với LicenseCheckActivity.

Nếu bạn cần lớp ExampleActivity để mở rộng ListActivity, thì bạn có thể có LicenseCheckActivity mở rộng ListActivity thay vì hoạt động.

+0

Tôi đang thử cùng một mã nhưng mỗi lần ứng dụng khởi chạy nó chỉ hiển thị cho tôi thông báo bánh mì nướng "Kiểm tra giấy phép xác minh" và bắt đầu bình thường. nó không hiển thị cho tôi cuộc đối thoại của mặt hàng mua như tôi đã không tải nó lên trên google play. – user2592807

Các vấn đề liên quan