2012-01-14 9 views
3

私はアプリを持っており、コピーライトの代わりにGoogleのライセンスを追加しようとしています。サーバー/サービスを使用しないAndroidライセンスチェック

私の問題は次のとおりです: デバイスが接続できるときは問題はなく、ライセンスがあればアクセスが許可されます。デバイスが接続でき、ライセンスがない場合、デバイスはアクセスを拒否します。

しかし、飛行機モードやデッドゾーンなどの理由でデバイスが接続できない場合は、ライセンスされていない応答が表示されます。

ここに私のコードです: パッケージ "パッケージ名";

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; 


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 =  "MY KEY"; 

private static final byte[] SALT = new byte[] { "20 RANDOM INTEGERS" }; 
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 the 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, MY_Activity.class)); 
    finish(); 
} 

public void toast(String string) { 
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); 
} 

} 

私はそれだけで戻っライセンスは来ていない場合にのみアクセスをブロックすることでも喜んで。 アプリが稼働し、サーバーが利用可能になるたびに、私はずっとチェックしたいと思います。 サーバーに到達してそこに行くことができるかどうかを確認するためにチェックを実行できたとしても。

答えて

1

私は回避策を見つけました。私は修正を見つけませんでした。徹底的な調査の結果、デフォルトでは接続がないか飛行機モードであれば、ライセンスチェックは自動的にNOT_LICENSEDにデフォルト設定されています。

私は接続性をチェックしました。接続があれば、ライセンスチェックが実行されます。接続がない場合は、メインアクティビティに移動します。

1

おそらく、LVLライブラリのテストアカウントを使用している可能性があります。 Google Testアカウントを使用している場合は、1分後にライセンスが有効になります。その後、有効な応答を返すためには、ライセンスの確認にネットアクセスが必要になります。アプリを購入した実際のアカウントでは、有効なライセンス応答が数日間キャッシュされます(ServerManagedPolicyを使用していると仮定します)。

0
public void dontAllow(int policyReason) { 

    if (isFinishing()) { 
     // Don't update UI if Activity is finishing. 
     return; 
    } 

    if (policyReason == Policy.RETRY) { 
     //-- no connection (unable to validate license). 
    } else { 
     //-- no license (application is not licensed). 
    } 

    showDialog(0); 
} 
関連する問題