6

Je suis sur le point de terminer mon application Android. Maintenant, je veux télécharger mon application sur le marché Android pour une application payante. Pour que j'ai lu le document à partir du site Android:Comment obtenir une licence pour mon application Android?

android licensing

Mais je veux voir une démonstration ou un projet qui a fait cette licence. J'ai vu la démo sur le site de développeur android de mainactivity. Mais là, le gestionnaire est donné et je veux implémenter ce code dans mymainactivity du projet. Dans mymainactivity, il y a un écran de démarrage au début de l'application, et j'ai défini un gestionnaire pour cela. C'est pourquoi j'ai besoin d'un exemple, pour voir comment implémenter la licence dans notre propre application.

Une autre chose que je veux savoir est, est-ce obligatoire de mettre en œuvre la licence Android afin de télécharger une application payante sur le marché Android?

Est-il possible de définir l'application comme payée sans implémenter la licence de l'application Android?
Si oui et s'il y a des démos disponibles, donnez-moi un lien.

+0

voir ma question donnée ici je veux la même fonctionnalité et le code d'activité est également collé là http://stackoverflow.com/questions/16169622/android-licensing-application-not-works – Khan

Répondre

19

Avant de commencer, vous avez inclus la bibliothèque de licence dans votre projet comme expliqué ici: Licensing Your Applications | Android Developers

  1. Faire une nouvelle activité dans votre projet appelé LicenseCheck.java

  2. Collez le texte suivant cette activité:

    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. Changer la clé de base à une offre google d, Placez 20 entiers aléatoires dans le SALT, Change MainActivity.class à l'activité principale de votre application.

  4. Mettez à jour votre fichier Manifest avec la nouvelle activité

    <!-- 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. Ajouter la permission dans la balise manifeste, mais pas dans la balise d'application

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

Vous avez terminé! Assurez-vous de le tester avant de publier. :) :)

+0

Je garde erreur, "Erreur: (48, 0) La description com.android.vending.CHECK_LICENSE n'est pas valide". Avez-vous une chance de savoir pourquoi? Merci beaucoup. – cjayem13

Questions connexes