简体   繁体   English

如何将许可证集成到其Android应用程序?

[英]How to integrate a license to its Android application?

I have a problem with my application: When it opens, it crashes due to the integration of license. 我的应用程序有一个问题:当它打开时,由于许可证的集成而崩溃。 But I do not know where the problem is. 但我不知道问题出在哪里。 Please can you help me find it? 请您能帮我找到它吗? I will be very grateful! 我将非常感谢!

My main acitivity: 我的主要爱好:

package com.example.testouille;

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;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;

public class MainActivity extends Activity {

     static final String BASE64_PUBLIC_KEY = "LICENSE";
     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 Handler mHandler;
     private LicenseChecker mChecker;
     private LicenseCheckerCallback mLicenseCheckerCallback;
     boolean licensed;
     boolean checkingLicense;
     boolean didCheck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



private void doCheck() {

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

    mChecker.checkAccess(mLicenseCheckerCallback);
}


private class MyLicenseCheckerCallback implements LicenseCheckerCallback {

    @Override
    public void allow(int reason) {

        if (isFinishing()) {

            return;
        }               
        Log.i("License","Accepted!");       



        licensed = true;
        checkingLicense = false;
        didCheck = true;

    }

    @SuppressWarnings("deprecation")
    @Override
    public void dontAllow(int reason) {

         if (isFinishing()) {

                return;
            }
            Log.i("License","Denied!");
            Log.i("License","Reason for denial: "+reason);                                                                              



            licensed = false;
            checkingLicense = false;
            didCheck = true;               

            showDialog(0);

    }

    @SuppressWarnings("deprecation")
    @Override
    public void applicationError(int reason) {

        Log.i("License", "Error: " + reason);
        if (isFinishing()) {

            return;
        }
        licensed = true;
        checkingLicense = false;
        didCheck = false;

        showDialog(0);
    }


}

protected Dialog onCreateDialog(int id) {

    return new AlertDialog.Builder(this)
            .setTitle("UNLICENSED APPLICATION DIALOG TITLE")
            .setMessage("This application is not licensed, please buy it from the play store.")
            .setPositiveButton("Buy", 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("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            })
            .setNeutralButton("Re-Check", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    doCheck();
                }
            })

            .setCancelable(false)
            .setOnKeyListener(new DialogInterface.OnKeyListener(){
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    Log.i("License", "Key Listener");
                    finish();
                    return true;
                }
            })
            .create();
}{
    String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
    Log.i("Device Id", deviceId);  
    mHandler = new Handler();
    mLicenseCheckerCallback = new MyLicenseCheckerCallback();
    mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new   AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY);
}}

My logcat: 我的日志:

02-21 15:00:06.530: D/AndroidRuntime(1231): Shutting down VM
02-21 15:00:06.530: W/dalvikvm(1231): threadid=1: thread exiting with uncaught exception (group=0xb3ab8ba8)
02-21 15:00:06.550: E/AndroidRuntime(1231): FATAL EXCEPTION: main
02-21 15:00:06.550: E/AndroidRuntime(1231): Process: com.example.testouille, PID: 1231
02-21 15:00:06.550: E/AndroidRuntime(1231): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testouille/com.example.testouille.MainActivity}: java.lang.NullPointerException
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.os.Looper.loop(Looper.java:136)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at java.lang.reflect.Method.invoke(Method.java:515)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at dalvik.system.NativeStart.main(Native Method)
02-21 15:00:06.550: E/AndroidRuntime(1231): Caused by: java.lang.NullPointerException
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at com.example.testouille.MainActivity.<init>(MainActivity.java:152)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at java.lang.Class.newInstanceImpl(Native Method)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at java.lang.Class.newInstance(Class.java:1208)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-21 15:00:06.550: E/AndroidRuntime(1231):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-21 15:00:06.550: E/AndroidRuntime(1231):     ... 11 more
02-21 15:00:14.370: I/Process(1231): Sending signal. PID: 1231 SIG: 9
02-21 15:01:30.460: D/AndroidRuntime(1251): Shutting down VM
02-21 15:01:30.470: W/dalvikvm(1251): threadid=1: thread exiting with uncaught exception (group=0xb3ab8ba8)
02-21 15:01:30.530: E/AndroidRuntime(1251): FATAL EXCEPTION: main
02-21 15:01:30.530: E/AndroidRuntime(1251): Process: com.example.testouille, PID: 1251
02-21 15:01:30.530: E/AndroidRuntime(1251): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testouille/com.example.testouille.MainActivity}: java.lang.NullPointerException
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.os.Looper.loop(Looper.java:136)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at java.lang.reflect.Method.invoke(Method.java:515)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at dalvik.system.NativeStart.main(Native Method)
02-21 15:01:30.530: E/AndroidRuntime(1251): Caused by: java.lang.NullPointerException
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at com.example.testouille.MainActivity.<init>(MainActivity.java:152)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at java.lang.Class.newInstanceImpl(Native Method)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at java.lang.Class.newInstance(Class.java:1208)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-21 15:01:30.530: E/AndroidRuntime(1251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-21 15:01:30.530: E/AndroidRuntime(1251):     ... 11 more
02-21 15:01:33.890: I/Process(1251): Sending signal. PID: 1251 SIG: 9

I am a "noob" in Java on Android ^^ . 我是Android ^^上Java的“菜鸟”。 Thanks you so much , David 非常感谢你,大卫

I know it's been awhile, however if you or someone else is having a problem with the code this way I am fairly sure the error is down where you are getting ready to make you dialog to let the use it fail to get a good license response. 我知道已经有一段时间了,但是,如果您或其他人以这种方式对代码有问题,我可以肯定错误已经解决,您正准备使您的对话框让使用该对话框无法获得良好的许可证响应。

protected Dialog onCreateDialog(int id) {

    return new AlertDialog.Builder(this)

Because it is in a class of its own and note in the class 因为它在自己的班级中并且在班级中有注释

class MainActivity extends Activity

You can't use "this" because the AlertDialog.Builder requires a Context, you would need something like: 您不能使用“ this”,因为AlertDialog.Builder需要一个Context,因此您需要:

protected Dialog onCreateDialog(int id) {

    new AlertDialog.Builder(getApplicationContext())

Another option would be to make a public context variable in the MainActivity the you could simply use that. 另一个选择是在MainActivity中创建一个公共上下文变量,您可以简单地使用它。

class MainActivity extends Activity{

   public Context context;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main); 
       this.context = this;
   }

Then down where you create the Dialog you can use the public context from the MainActivity like this: 然后在创建对话框的位置可以使用MainActivity的公共上下文,如下所示:

protected Dialog onCreateDialog(int id) {

    new AlertDialog.Builder(context)

我唯一能想到的是,如果您没有为应用程序提供运行所需的权限,但我显然比您还好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM