我怎样才能实现为Android授权策略? [英] How can I implement a Licensing strategy for Android?

查看:177
本文介绍了我怎样才能实现为Android授权策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到股票Android的开发者许可库说明,但我怎样才能实现真正的应用程序的策略是什么?

I saw the stock Android-Developer licensing library instructions, but how can I implement the strategy in a real application?

推荐答案

我一直在我的应用程序有一段时间了实施许可和终于等来了它的工作。我想分享一些我发现有助于入门的东西,一些问题和解决方案,我发现每个人。 Android开发教程下面我有联系是好的,但它是没有多大用处的我,所以我决定做一个教程。享受,我希望它可以帮助你!

I have been working on implementing licensing in my app for some time now and finally have it working. I wanted to share some of the things I found helpful for getting started and some problems and solutions that I found with everyone. The android dev tutorial I have linked below is ok, but it wasn't that useful for me, so I decided to make a tutorial. Enjoy, and I hope it helps you!

链接到这里开发的网页。

Link to developer page here.

1。入门

的事情。

1.1您的Base64独特的应用关键

如何获得它:

一个。转到您的开发者控制台。 链接。

a. Go to your developer console. Link.

乙。如果您尚未创建的应用程序草案,供您的应用程序,现在就这样做。

b. If you haven't already created an application draft for your app, do it now.

℃。一旦你创建了草案,但它是上传你的 .apk文件为α或β是个好主意。离开它未公开。

c. Once you have created the draft, it is a good idea to upload your .apk as Alpha or Beta. Leave it unpublished.

Ð。点击服务和放大器;原料药

如然后找到许可证密钥此应用程序

F。钥匙复制到您的应用程序是这样的:

f. Copy the key into your app like this:

private static final String BASE64_PUBLIC_KEY = "YOUR LICENSE KEY FOR THIS APPLICATION"; 

确认,目前并无任何空间。

Make sure that there are no spaces.

1.2的盐

一个。什么是盐?

一个是随机数据散列密码时,这是额外的输入。它们被用来对抗字典攻击的防守和的彩虹表攻击。

A salt is random data that is additional input when hashing a password. They are used to defend against dictionary attacks and rainbow table attacks.

乙。我如何获得?

是生成一个随机的盐一个很好的链接。应该有完全 20个随机整数,所以就把 20 的随机字符串生成量,每个字符串应该是 2 字符长(用于本例,它不必须是)。检查位数字,并检查被允许相同的字符串。它们可以是负数太。尝试删除任何冗余,如 00 - > 0 ,为求一致。

This is a good link to generate a random salt. There should be exactly 20 random integers, so put 20 in for the amount of random strings to generate, each string should be 2 characters long (used for this example, it doesn't have to be). Check numeric digits, and check Identical strings are allowed. They can be negative numbers too. Try to remove any redundancy, e.g. 00 -> 0, for the sake of consistency.

℃。我在哪里把盐?

在声明变量只是把这个code,除了你的随机盐。

When declaring variables just put this code in, except with your random salt.

private static final byte[] SALT = new byte[] {YOUR RANDOM SALT, COMMA SEPARATED, 20 INTEGERS};

2。导入拉特(牌)库到Eclipse和code,你需要

2.1导入库

一个。打开 Android的SDK管理器

乙。转到其他

℃。安装谷歌播放许可库

Ð。寻找你的 SDK 安装的列在SDK管理器顶部的路径。

d. Find your SDK install path which is listed at the top of the SDK manager.

如一旦你在那里,定位到:< SDK> /演员/谷歌/ play_licensing

e. Once you are there, navigate to: <sdk>/extras/google/play_licensing

F。在Eclipse中单击文件然后进口,然后现有的Andr​​oid code进入工作区键,当它要求您输入文件路径,浏览到 play_licensing 文件夹,然后点击

f. In eclipse, click file then import, then Existing Android Code Into Workspace and when it asks you for the file path, navigate to the play_licensing folder and click on library.

克一旦这个项目命名为已导入,右键单击它,然后点击属性。点击左侧安卓并导航至底部,并检查是库,然后点击应用。这让月食知道,你可以使用这个项目code作为一个库。

g. Once the project named library has been imported, right click it, then hit properties. Click Android on the left and navigate to the bottom and check Is Library, then hit apply. This lets eclipse know that you can use this project code as a library.

小时。右键单击您的应用程序要添加授权来,然后点击属性,然后点击安卓。进入底部,然后点击,并将其添加到构建路径。这应该导入库的 Android的依赖文件夹。

h. Right click on your app that you are adding licensing to, and click properties, then hit Android. Go to the bottom and click library and add it to the build path. This should import the library to the Android Dependencies folder.

我。您的项目设置为进入下一个步骤。

i. Your project is set up to go to the next step.

2.2变量与您 KEY声明以及

2.2 Variables to declare along with your SALT and KEY

private Handler mHandler;
private LicenseChecker mChecker;
private LicenseCheckerCallback mLicenseCheckerCallback;
boolean licensed;
boolean checkingLicense;
boolean didCheck;

2.3 code

粘贴您的应用程序底部附近的这个code。此实现将通知用户是否许可无效,并促使他们购买的应用程序或退出。

Paste this code near the bottom of your app. This implementation will notify the user if the license is not valid and prompt them to buy the app or exit it.

    private void doCheck() {

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

        mChecker.checkAccess(mLicenseCheckerCallback);
    }


    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {

        @Override
        public void allow(int reason) {
            // TODO Auto-generated method stub
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }               
            Log.i("License","Accepted!");       

                //You can do other things here, like saving the licensed status to a
                //SharedPreference so the app only has to check the license once.

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

        }

        @SuppressWarnings("deprecation")
        @Override
        public void dontAllow(int reason) {
            // TODO Auto-generated method stub
             if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }
                Log.i("License","Denied!");
                Log.i("License","Reason for denial: "+reason);                                                                              

                        //You can do other things here, like saving the licensed status to a
                        //SharedPreference so the app only has to check the license once.

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

                showDialog(0);

        }

        @SuppressWarnings("deprecation")
        @Override
        public void applicationError(int reason) {
            // TODO Auto-generated method stub
            Log.i("License", "Error: " + reason);
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            licensed = true;
            checkingLicense = false;
            didCheck = false;

            showDialog(0);
        }


    }

    protected Dialog onCreateDialog(int id) {
        // We have only one dialog.
        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();

    }

2.4让您的设备ID

目前已在过去对是否使用SIM卡序列号或 TelephonyManager.getDeviceId()关于这方面的一些争论; ,但它通常建议您使用下面code,以获得 ANDROID_ID 您的设备最大限度的兼容性。

There has been some debate about this in the past about whether or not to use the sim serial or TelephonyManager.getDeviceId(); but it is generally recommended you use the following code to get the ANDROID_ID of your device for maximum compatibility.

String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
Log.i("Device Id", deviceId);  //AN EXAMPLE OF LOGGING THAT YOU SHOULD BE DOING :)

2.5创建的许可证检查

一个。致电前 doCheck(); 你必须把这个code。在您的应用程序,以确保一切得到正确创建

a. Before you call doCheck(); you must put this code in your app to make sure everything gets created properly.

mHandler = new Handler();
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new   AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY);

当我在做我的拉特的实现,我读了,如果你有问题许可,您可以更改第一个 mChecker =新LicenseChecker(这... getApplicationContext(),煤矿似乎没有它的工作,但为了以防万一。

When I was doing my implemetation of LVL, I read that if you are having problems with Licensing, you can change the first this in the mChecker = new LicenseChecker(this... to getApplicationContext(), mine seemed to work without it, but just in case.

2.6添加权限

一个。有迹象表明,你需要添加到您的应用程序清单文件。

a. There are two permissions that you need to add to your applications manifest file.

<uses-permission android:name="android.permission.INTERNET"/>  
<uses-permission android:name="com.android.vending.CHECK_LICENSE"/>        

2.7请确保您有适当的进口!

您可能已经做到了这一点,但我想这将是一个很好的地方给你检查一下。

You have probably already done this, but I figured it would be a good place for you to check.

2.8如何调用许可证进行检查

一个。只需拨打 doCheck(); 时,你要检查的许可证。例如,如果应用程序是在它第一次运行,做检查。

a. Simply call doCheck(); whenever you want to check the license. For example if the app is on its first run, do the check.

3。如何测试许可,使出版它之前,请确保它的工作原理?

3.1配置测试设备

一个。我有我的个人电话,我也用于测试。建议有注册的手机上只有一个谷歌帐户,在历史上它使事情变得更容易一些。您可以将设置检查账户 - &GT;帐户

a. I have my personal phone that I also use for testing. It is recommended that there be only one Google account registered on the phone, historically it makes things a little easier. You can check the accounts by going to Settings -> Accounts.

3.2配置开发者控制台

一个。打开你的开发者控制台,转到设置的左侧。

a. Open your developer console and go to Settings on the left hand side.

乙。找到许可测试

℃。请确保您的电子邮件地址被列在的Gmail账户的测试访问

c. Make sure that your email address is listed under Gmail accounts with testing access

Ð。现在,您可以更改测试应对任何你喜欢的用于测试目的。应用程序应作出相应的反应。请记住,如果你是通过共享preFS保存数据,你需要在每次测试的时间来清除你的应用程序的数据。 请确保您单击保存更改测试响应或什么都不会发生了!我忘了这个多次,我结束了偏头痛,然后我看到那臭烘烘的保存按钮。大声笑。

d. Now, you can change the test response to whatever you like for testing purposes. The app should respond accordingly. Remember that if you are saving the data via SharedPrefs you will need to clear your app data every time you test it. Make sure that you click save after you change the test response or nothing will happen! I forgot about this multiple times and I ended up with a migraine, then I saw that stinking save button. Lol.

4。可以尝试

4.1条件许可检查

一个。你可以试试这个code。如果要保存在 didCheck 数据共享preferences

a. You can try this code if you are saving the didCheck data in SharedPreferences.

 if(didCheck==false){
        Toast.makeText(this, "Checking application license...",     Toast.LENGTH_SHORT).show();
        doCheck();
        Log.i("Checking!", "Checking license!");
    }   

4.2加密你的共享preferences 使用安全preferences

4.2 Encrypting your SharedPreferences using SecurePreferences

一个。去这个链接

乙。复制和粘贴安全preferences.java 的code为一类具有完全相同的名称为您的项目。

b. Copy and paste the code from SecurePreferences.java into a class with the exact same name into your project.

℃。阅读 ReadMe.md 有关实施这一信息。

c. Read the ReadMe.md for info on implementing this.

5。故障排除

许可可以是一个头痛的一个赫克解决,只是因为有很多事情可能出错。例如,有可能是网络问题或服务器的问题,让你想撕裂你的头发了。使用适当的日志记录后会解决这个问题,你也可以得到服务器的响应codeS如果有问题,你可以将它追踪到服务器或应用程序。我不得不这样做在多个场合。

Licensing can be one heck of a headache to troubleshoot, simply because there are many more things that can go wrong. For example, there could be network problems or server problems that make you want to rip your hair out. Use of proper logging will help with this, you can also get the server response codes if there is a problem and you can trace it to the server or your app. I have had to do this on multiple occasions.

5.1我不能让我的应用程序从服务器返回什么

可能的修正:

一个。请确保您的应用程序有正确的

a. Make sure that your app has the correct KEY.

乙。确保你正在登录的进程的每一步

b. Make sure you are logging each step of the progress

℃。检查你的日志从许可服务什么。它可以为搞清楚出事了有益的。

c. Check your log for anything from the licensing service. It can be useful for figuring out where something went wrong.

Ð。确保允许() dontAllow()的ApplicationError() @覆盖标签。

5.2我的应用程序总是说许可 NOT_LICENSED 不管我在测试其设置为响应

5.2 My app always says LICENSED or NOT_LICENSED no matter what I set it to in the test response

一个。最好的治疗我有这只是等待。看来,如果你做很多测试,在短时间内,它总是会送你的服务器code 291 这是重试code。我等了一夜,一切都在第二天早上正常工作。

a. The best cure I have for this is just to wait. It seems that if you do lots of testing in a short period of time, it will always send you server code 291 which is the retry code. I waited overnight and everything worked fine the next morning.

乙。您可以清除谷歌的数据(不只是缓存)播放应用程序和谷歌播放服务的应用程序。然后开打备份并接受所有许可证,然后重试。

b. You can clear the data (not just cache) of the Google Play app and the Google Play Services app. Then open play back up and accept all the licenses and try again.

℃。清除您的应用程序的数据。

c. Clear your app data.

5.3列表服务器响应codeS的调试

如果你登录他们应该获得 INT原因这些十进制值。使用此表来引用什么服务器实际发送到您的应用程序。

You should get these decimal values for int reason if you log them. Use this table to reference what the server is actually sending to your app.

LICENSED = Hex: 0x0100, Decimal: 256
NOT_LICENSED = Hex: 0x0231, Decimal: 561
RETRY = Hex: 0x0123, Decimal: 291
LICENSED_OLD_KEY = Hex: 0x2, Decimal: 2
ERROR_NOT_MARKET_MANAGED = Hex: 0x3, Decimal: 3
ERROR_SERVER_FAILURE = Hex: 0x4, Decimal: 4
ERROR_OVER_QUOTA = Hex: 0x5, Decimal: 5
ERROR_CONTACTING_SERVER = Hex: 0x101, Decimal: 257
ERROR_INVALID_PACKAGE_NAME = Hex: 0x102, Decimal: 258 
ERROR_NON_MATCHING_UID = Hex: 0x103, Decimal: 259

5.4更多空间!他们会来!

我希望这可以帮助你们!我试着用你们尽我所能分享我的头痛和修复。我希望这有助于!

I hope this helps you guys! I tried to share my headaches and fixes with you guys as best I can and I hope this helps!

如果我犯了任何错误,一定要告诉我,他们让我可以让他们尽快解决!

If I made any errors, be sure to tell me about them so I can get them fixed ASAP!

这篇关于我怎样才能实现为Android授权策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆