Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android授权教程-最后一步_Java_Android_Android Lvl - Fatal编程技术网

Java Android授权教程-最后一步

Java Android授权教程-最后一步,java,android,android-lvl,Java,Android,Android Lvl,我的应用程序现在可以提交了,但我最近了解了许可 我在网上找到了一个循序渐进的教程: 我已经将许可库导入Eclipse,并按照教程中的描述创建了LicenseCheckActivity类 我在教程的最后一步,第7点。教程说我的类应该扩展LicenseCheckActivity。然而,我想要检查许可的类已经扩展了活动 如何从我的LicenseCheckActivity类中使用checkLicense()方法 这是我的密码: protected void onCreate(Bundle savedIn

我的应用程序现在可以提交了,但我最近了解了许可

我在网上找到了一个循序渐进的教程:

我已经将许可库导入Eclipse,并按照教程中的描述创建了
LicenseCheckActivity

我在教程的最后一步,第7点。教程说我的类应该扩展
LicenseCheckActivity
。然而,我想要检查许可的类已经扩展了活动

如何从我的
LicenseCheckActivity
类中使用
checkLicense()
方法

这是我的密码:

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();
这给了我以下错误:

无法实例化类型LicenseCheckActivity

这是我的
LicenseCheckActivity
课程

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

}

您得到该错误是因为您正在尝试

 // Check the license
LicenseCheckActivity  l = new LicenseCheckActivity();
举例说明一项活动。你从来没有这样做过!始终使用ActivityManager和Intents启动活动并在它们之间传递信息

解决方案:

由于您希望start类继续扩展活动,并且不能让它扩展
LicenseCheckActivity
,因此唯一的其他建议是在start类中移动代码

例:


LicenseCheckActivity
中的所有代码移到
MainActivity
类中,然后您可以在
MainActivity

的onCreate方法中调用
checkLicense()
教程说明使用
LicenseCheckActivity
扩展
活动

例如:

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
扩展了
Activity
,如果您使用
LicenseCheckActivity
扩展类,则类
ExampleActivity
仍将继承您的
Activity
方法


如果您需要您的
ExampleActivity
类来扩展
ListActivity
,那么您可以使用
LicenseCheckActivity
扩展
ListActivity
而不是activity。

LicenseCheckActivity
似乎是
静态的。可以尝试使用
LicenseCheckActivity.checkLicense()
@Mualig我已尝试了您的代码,但出现以下错误:无法从类型LicenseCheckActivity对非静态方法checkLicense()进行静态引用。Eclipse提供了一个快速修复方法:将CheckLicense()的修饰符更改为“static”。但是checklicense中使用的AESObfuscator不接受静态引用。我正在尝试相同的代码,但每次应用程序启动时,它都只显示toast消息“License check verification”,并且程序正常启动。它没有显示购买物品的对话,因为我没有上传到google play上。