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

相關問題