0

我一直在尝试向我的应用程序添加许可支持,但没有收到任何错误,但是 logcat 说它正在检查许可,但它在应用程序中没有做任何事情。当应用程序打开时,它做的第一件事就是检查许可证。我得到的最后一个 logcat 条目是“收到的响应”和“清除超时”。如果响应是 NOT_LICENSED 或 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();

    }
4

1 回答 1

0

好吧,我终于想通了,我必须重写 allow() 和 dontAllow() 才能让它工作!

于 2013-08-18T19:35:32.690 回答