我无法获得许可以返回LICENSED或NOT_LICENSED或Android中的任何内容

时间:2013-08-18 05:19:44

标签: android android-lvl

我一直在尝试为我的应用添加许可支持,但我没有收到任何错误,但是logcat说它正在检查许可证,但它在应用程序中没有做任何事情。当应用程序打开时,它首先要检查许可证。我得到的最后一个logcat条目是“Received Response”和“Clearing timeout”。如果响应是NOT_LICENSED或许可发生,我已经设置了做某些事情的实际处理,我已经设置了记录。这可能是一些明显的问题,我没有看到,因为我是一个新的开发人员。是的我正确导入了库。

public class MainActivity extends ListActivity {

public static final String PREFS_NAME = "MyPrefsFile";
private ArrayAdapter<String> adapter;
private ListView list;  
private Context c = this;
boolean home;
boolean school;

boolean licensed = false;
boolean didCheck = false;
boolean checkingLicense = false;

SharedPreferences userIsLicensed=null;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);    

    userIsLicensed = getSharedPreferences(PREFS_NAME, 0);    
    licensed = userIsLicensed.getBoolean("licensed", false);

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

//许可证检查部分

    String BASE64_PUBLIC_KEY = "KEY";

    LicenseCheckerCallback mLicenseCheckerCallback;
    LicenseChecker mChecker;

    Handler mHandler;

    // REPLACE WITH YOUR OWN SALT , THIS IS FROM EXAMPLE
    private final byte[] SALT = new byte[]{BYTES};

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

    protected class MyLicenseCheckerCallback implements LicenseCheckerCallback {

        public void allow() {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }

            Log.i("License","Accepted!");
            // Should allow user access.
            displayResult(getString(R.string.allow));
            licensed = true;
            checkingLicense = false;
            didCheck = true;

            SharedPreferences.Editor editor = userIsLicensed.edit();
            editor.putBoolean("licensed", true);
            editor.commit();

            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);

        }

        @SuppressWarnings("deprecation")
        public void dontAllow() {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            Log.i("License","Denied!");
            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;

            SharedPreferences.Editor editor = userIsLicensed.edit();
            editor.putBoolean("licensed", false);
            editor.commit();

            showDialog(0);
        }

        @SuppressWarnings("deprecation")
        public void applicationError(ApplicationErrorReport 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.
            @SuppressWarnings("unused")
            String result = String.format(getString(R.string.application_error), errorCode);
            checkingLicense = false;
            didCheck = true;

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

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

        }

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

        }

        @Override
        public void applicationError(int errorCode) {
            // 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();

    }

1 个答案:

答案 0 :(得分:0)

好吧,我终于明白了,我不得不重写allow()和dontAllow()才能让它发挥作用!