6

我即将完成我的Android应用程序。现在我想将我的应用程序上传到Android市场以获得付费应用程序。对于那些我已经从Android网站阅读文档:如何授权我的Android应用程序?

android licensing

但我希望看到曾做过这样的许可任何演示或项目。我已经看到了主要活动的android开发人员网站上的演示。但是那里给出了处理程序,我想将该代码实现到我的项目的主要活动中。 在mymainactivity中,在应用程序的开始处有一个启动画面,并且我为它设置了一个处理程序。所以这就是为什么我需要一个例子,看看如何在我们自己的应用程序中实现许可。

我想知道的另一件事是,为了将付费应用程序上传到Android市场,是否必须实施Android许可?

是否可以将应用程序设置为付费而不实施Android应用程序的许可?
如果有,并且有任何演示可用,请给我一个链接。

+0

看到我在这里给出的问题我想同样的功能和活动代码也粘贴在那里http://stackoverflow.com/questions/16169622/android-licensing-application-not-works – Khan

回答

19

在开始之前,请您已经包括在您的项目许可证库作为解释在这里: Licensing Your Applications | Android Developers

  1. 请在您的项目中的新活动称为LicenseCheck.java

  2. 粘贴以下在该活动中:

    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.widget.Toast; 
    import com.android.vending.licensing.AESObfuscator; 
    import com.android.vending.licensing.LicenseChecker; 
    import com.android.vending.licensing.LicenseCheckerCallback; 
    import com.android.vending.licensing.ServerManagedPolicy; 
    
    /** 
    * NOTES ON USING THIS LICENSE FILE IN YOUR APPLICATION: 
    * 1. Define the package 
    * of you application above 
    * 2. Be sure your public key is set properly @BASE64_PUBLIC_KEY 
    * 3. Change your SALT using random digits 
    * 4. Under AllowAccess, Add your previously used MainActivity 
    * 5. Add this activity to 
    * your manifest and set intent filters to MAIN and LAUNCHER 
    * 6. Remove Intent Filters from previous main activity 
    */ 
    public class LicenseCheck extends Activity { 
    private class MyLicenseCheckerCallback implements LicenseCheckerCallback { 
    @Override 
    public void allow() { 
         if (isFinishing()) { 
             // Don't update UI if Activity is finishing. 
             return; 
    } 
    // Should allow user access. 
    startMainActivity(); 
    
          } 
    
    @Override 
    public void applicationError(ApplicationErrorCode errorCode) { 
        if (isFinishing()) { 
         // Don't update UI if Activity is finishing. 
         return; 
        } 
        // 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. 
        toast("Error: " + errorCode.name()); 
        startMainActivity(); 
    
    } 
    
    @Override 
    public void dontAllow() { 
        if (isFinishing()) { 
         // Don't update UI if Activity is finishing. 
         return; 
        } 
    
        // 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. 
        showDialog(0); 
    } 
    } 
    private static final String BASE64_PUBLIC_KEY = "PLACE YOUR BASE KEY FROM GOOGLE HERE"; 
    private static final byte[] SALT = new byte[] { INPUT 20 RANDOM INTEGERS HERE }; 
    private LicenseChecker mChecker; 
    
    // A handler on the UI thread. 
    
    private LicenseCheckerCallback mLicenseCheckerCallback; 
    private void doCheck() { 
         mChecker.checkAccess(mLicenseCheckerCallback); 
    } 
    
    @Override 
        public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    
    // Try to use more data here. ANDROID_ID is a single point of attack. 
    String deviceId = Secure.getString(getContentResolver(), 
         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(); 
        } 
    
    @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("http://market.android.com/details?id=" 
                 + getPackageName())); 
             startActivity(marketIntent); 
             finish(); 
            } 
           }) 
         .setNegativeButton("Exit", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, 
              int which) { 
             finish(); 
            } 
           }).create(); 
        } 
        @Override 
        protected void onDestroy() { 
    super.onDestroy(); 
    mChecker.onDestroy(); 
        } 
    
        private void startMainActivity() { 
    startActivity(new Intent(this, MainActivity.class)); //REPLACE MainActivity.class WITH YOUR APPS ORIGINAL LAUNCH ACTIVITY 
    finish(); 
        } 
    
        public void toast(String string) { 
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); 
        } 
    
    } 
    
  3. 更改基本密钥为一个谷歌提供d,在SALT中放置20个随机整数,将MainActivity.class更改为应用程序的主要活动。

  4. 更新您的新活动

    <!-- Old Launch Activity Here --> 
    <activity android:label="@string/app_name" android:name=".MainActivity" /> 
    <!-- New License Launch Activity with all intent filters from your previous main activity --> 
    <!-- Translucent.NoTitleBar is so that this activity is never shown to the user -->  
    <activity android:label="@string/app_name" android:name=".LicenseCheck" 
        android:theme="@android:style/Theme.Translucent.NoTitleBar"> 
        <intent-filter> 
         <action android:name="android.intent.action.MAIN" /> 
         <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
    </activity> 
    
  5. 添加权限在清单标签,但不是在应用程序标签

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

大功告成清单文件!确保在发布之前对其进行测试。 :) :)

+0

我保持出现错误“错误:(48,0)说明com.android.vending.CHECK_LICENSE无效”。你知道为什么吗?非常感谢。 – cjayem13

相关问题