2012-01-14 87 views
3

我有一個應用程序,我試圖添加谷歌授權而不是版權保護。沒有服務器/服務可用的Android授權檢查

我的問題是: 當設備可以連接沒有問題,如果有許可證,則授予訪問權限。如果設備可以連接並且沒有許可證,則拒絕訪問。

但是當設備由於飛行​​模式,死區等而無法連接時,應用程序會給出未獲得許可的響應。

這裏是我的代碼: package「package name」;

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