2012-06-22 13 views
9

La mia app è pronta per essere inoltrata ora, ma di recente ho appreso le licenze.Tutorial per licenze Android - Passaggio finale

ho trovato un passo per passo per esercitazione in linea: http://twistbyte.com/tutorial/using-the-android-licensing-service-step-by-step

Ho importato la libreria Licensing in Eclipse e ha creato la classe LicenseCheckActivity come descritto nel tutorial.

Sono sul passaggio finale del tutorial, punto numero 7. Il tutorial dice che la mia classe dovrebbe estendere LicenseCheckActivity. Tuttavia, la classe in cui vorrei verificare le licenze estende già l'attività.

Come è possibile utilizzare il metodo checkLicense() dalla mia classe LicenseCheckActivity?

Ecco il mio codice:

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

Questo mi dà il seguente errore:

Cannot instantiate the type LicenseCheckActivity

Qui è la mia classe 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(); 
    } 
} 

} 
+0

'LicenceCheckActivity' sembra essere' statico'. Forse prova con 'LicenseCheckActivity.checkLicense();'. – Mualig

+0

@Mualig Ho provato il tuo codice e ottengo il seguente errore: Impossibile creare un riferimento statico al metodo non statico checkLicense() dal tipo LicenseCheckActivity. Eclipse offre una soluzione rapida: Cambia il modificatore di CheckLicense() in "statico".Ma l'AESObfuscator utilizzato in un check-check non accetta riferimenti statici. – tiptopjat

risposta

5

Hai trovato questo errore perché sei prova a

// Check the license 
LicenseCheckActivity l = new LicenseCheckActivity(); 

Istanza un'attività. Non lo fai mai! Usa sempre ActivityManager e Intents per iniziare le attività e passare le informazioni tra loro.

La soluzione:

Dal momento che si desidera che la classe di partenza per continuare l'estensione di attività e non può avere estendere LicenseCheckActivity l'unico altro suggerimento sarebbe quello di spostare il codice nella classe di partenza.

Es:

prendere tutto il codice dal tuo LicenseCheckActivity e spostarlo nella vostra classe MainActivity e poi si può chiamare checkLicense() nel metodo onCreate dei tuoi MainActivity

3

Gli stati tutorial per estendere la vostra Activity con LicenseCheckActivity .

Esempio:

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

Perché LicenseCheckActivity estende Activity, la classe ExampleActivity sarà ancora erediterà i tuoi Activity metodi se si estende con LicenseCheckActivity.

Se avete bisogno la classe ExampleActivity di estendere ListActivity, allora si potrebbe avere LicenseCheckActivity estendono ListActivity invece di attività.

+0

sto provando lo stesso codice ma ogni volta che l'app lo avvia mostra solo il messaggio "Verifica controllo licenza" e l'avvio è normale. non mi mostra il dialogo degli articoli acquistati in quanto non l'ho caricato sul google play. – user2592807

Problemi correlati