如何在android中添加许可功能?

时间:2012-08-06 09:43:04

标签: android

我想在我的项目中添加应用程序许可编码,这是我用于检查的编码,我创建了一个库项目,我将该库项目作为库添加到我的项目中,并且还添加了所需的权限项目清单但我仍在努力关闭

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.Log;
import android.widget.Toast;

import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.ServerManagedPolicy;


public class LicenseCheck extends Activity { 

    private LicenseChecker mChecker;
    private LicenseCheckerCallback mLicenseCheckerCallback;
    private static final String BASE64_PUBLIC_KEY = "The key i got from the market";
 // Generate 20 random bytes, and put them here.
    private static final byte[] SALT = new byte[] {
     -20, 30, 50, -70, 33, -100, 32, -90, -88, 104, 12,
     -10, 72, -34, 115, 21, 62, 35, -12, 97
     };
    private AESObfuscator mObsfuscator;
    private String android_id;

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
         super.onCreate(icicle); 
         setContentView(R.layout.splash);
         android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
         mObsfuscator = new AESObfuscator(SALT, getPackageName(), android_id);
         ServerManagedPolicy serverPolicy = new ServerManagedPolicy(this,mObsfuscator);

         mLicenseCheckerCallback = new MyLicenseCheckerCallback();
         mChecker = new LicenseChecker(
             this, serverPolicy,
             BASE64_PUBLIC_KEY  // Your public licensing key.
             );
         mChecker.checkAccess(mLicenseCheckerCallback);  
    }

private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
        public void allow(int reason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            // Should allow user access.
            Log.w("LicenseChecker", "Allow");
            Intent i = new Intent(LicenseCheck.this, App1_1Activity.class);
            startActivity(i);
            finish();
        }

        public void dontAllow(int reason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            Log.w("LicenseChecker", "Don't Allow");
            // Should not allow access. An app can handle as needed,
            // typically by informing the user that the app is not licensed
            // and then shutting down the app or limiting the user to a
            // restricted set of features.
            // In this example, we show a dialog that takes the user to Market.
            showDialog(0);

        }

//      @Override
        public void applicationError(int errorCode) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
//          toast("Error: " + errorCode.name());

        }
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        // We have only one dialog.
        return new AlertDialog.Builder(this)
                .setTitle("Application Not Licensed")
                .setCancelable(false)
                .setMessage(
                        "This application is not licensed. Please purchase it from Android Market")
                .setPositiveButton("Buy App",
                        new DialogInterface.OnClickListener() {
//                          @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Intent marketIntent = new Intent(
                                        Intent.ACTION_VIEW,
                                        Uri.parse("market://details?id=" + getPackageName()));
                                startActivity(marketIntent);
                                finish();
                            }
                        })
                .setNegativeButton("Exit",
                        new DialogInterface.OnClickListener() {
//                          @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                finish();
                            }
                        }).create();
    }   

    public void toast(String string) {
        Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    }

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

这是logcat消息

    08-06 15:01:33.301: E/AndroidRuntime(887): FATAL EXCEPTION: main
08-06 15:01:33.301: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iexemplar.pray4me/com.iexemplar.pray4me.LicenseCheck}: java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.io.IOException: corrupted stream - out of bounds length found
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.os.Looper.loop(Looper.java:123)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-06 15:01:33.301: E/AndroidRuntime(887):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 15:01:33.301: E/AndroidRuntime(887):  at java.lang.reflect.Method.invoke(Method.java:507)
08-06 15:01:33.301: E/AndroidRuntime(887):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-06 15:01:33.301: E/AndroidRuntime(887):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-06 15:01:33.301: E/AndroidRuntime(887):  at dalvik.system.NativeStart.main(Native Method)
08-06 15:01:33.301: E/AndroidRuntime(887): Caused by: java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.io.IOException: corrupted stream - out of bounds length found
08-06 15:01:33.301: E/AndroidRuntime(887):  at com.google.android.vending.licensing.LicenseChecker.generatePublicKey(LicenseChecker.java:121)
08-06 15:01:33.301: E/AndroidRuntime(887):  at com.google.android.vending.licensing.LicenseChecker.<init>(LicenseChecker.java:92)
08-06 15:01:33.301: E/AndroidRuntime(887):  at com.iexemplar.pray4me.LicenseCheck.onCreate(LicenseCheck.java:45)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-06 15:01:33.301: E/AndroidRuntime(887):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-06 15:01:33.301: E/AndroidRuntime(887):  ... 11 more
08-06 15:01:33.301: E/AndroidRuntime(887): Caused by: java.security.spec.InvalidKeySpecException: java.io.IOException: corrupted stream - out of bounds length found
08-06 15:01:33.301: E/AndroidRuntime(887):  at org.bouncycastle.jce.provider.JDKKeyFactory.engineGeneratePublic(JDKKeyFactory.java:92)
08-06 15:01:33.301: E/AndroidRuntime(887):  at org.bouncycastle.jce.provider.JDKKeyFactory$RSA.engineGeneratePublic(JDKKeyFactory.java:396)
08-06 15:01:33.301: E/AndroidRuntime(887):  at java.security.KeyFactory.generatePublic(KeyFactory.java:177)
08-06 15:01:33.301: E/AndroidRuntime(887):  at com.google.android.vending.licensing.LicenseChecker.generatePublicKey(LicenseChecker.java:112)
08-06 15:01:33.301: E/AndroidRuntime(887):  ... 15 more
08-06 15:01:39.001: I/Process(887): Sending signal. PID: 887 SIG: 9

有人帮我解决了这个问题

1 个答案:

答案 0 :(得分:1)

您已为MyLicenseCheckerCallback编写了一个类,但尚未实例化它的实例。

相反,您有一个LicenseCheckerCallback实例。

所以你正在使用LVLs实现而不是你自己的。