編輯:初級開發
1. 在項目的androidmanifest.XML文件中加入<uses-permission android:name="com.android.vending.CHECK_LICENSE">權限,使其Market客戶端可以檢測授權狀態。
2.制定一個策略Policy,本質上 android Market licensing 服務並沒有判斷給用戶賦予什麼樣的權限,在Policy接口中提供了兩個方法分別為allowAccess() 和 processServerResponse() 他們均被 LicenseChecker 從服務器響應權限信息明確時被回調。
processServerResponse() 的原始定義 lets you preprocess the raw response data received from the licensing server, prior to determining whether to grant Access.
allowAccess() 的解釋為determines whether to grant the user Access to your application, based on any available license response data (from the licensing server or from cache) or other application-specific information.
3. 添加導入
使用默認的策略時導入
import com.android.vending.licensing.LicenseChecker;
import com.android.vending.licensing.LicenseCheckerCallback;
當然我們自定義策略,比如ServerManagedPolicy或AESObfuscator則應該導入
import com.android.vending.licensing.ServerManagedPolicy;
import com.android.vending.licensing.AESObfuscator;
4. 我們實現LicenseCheckerCallback接口為一個私有的內部類,比如代碼
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
public void allow() {
if (isFinishing()) {
return;
}
displayResult(getString(R.string.allow));
}
public void dontAllow() {
if (isFinishing()) {
return;
}
displayResult(getString(R.string.dont_allow));
}
}
上面僅實現allow和dontAllow兩個方法,但是可能會有網絡錯誤或Market服務器問題,所以我們還要重寫applicationError()方法獲取錯誤信息,服務器主要的響應有LICENSED、LICENSED_OLD_KEY、NOT_LICENSED、ERROR_CONTACTING_SERVER、ERROR_SERVER_FAILURE、ERROR_INVALID_PACKAGE_NAME、ERROR_NON_MATCHING_UID、ERROR_NOT_MARKET_MANAGED具體的方法,可以通過類似
public boolean allowAccess() {
long ts = System.currentTimeMillis();
if (mLastResponse == LicenseResponse.LICENSED) {
// Check if the LICENSED response occurred within the validity timeout.
if (ts <= mValidityTimestamp) {
// Cached LICENSED response is still valid.
return true;
}
} else if (mLastResponse == LicenseResponse.RETRY &&
ts < mLastResponseTime + MILLIS_PER_MINUTE) {
// Only allow Access if we are within the retry period or we haven't used up our
// max retrIEs.
return (ts <= mRetryUntil || mRetryCount <= mMaxRetrIEs);
}
return false;
}
5. 處理邏輯
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState)
{
mHandler = new Handler();
//DOSomething
}
private void displayResult(final String result)
{
mHandler.post(new Runnable()
{
public void run()
{
mStatusText.setText(result);
setProgressBarIndeterminateVisibility(false);
mCheckLicenseButton.setEnabled(true);
}
});
}
有關主類的Activity這樣添加
public class MainActivity extends Activity {
private LicenseCheckerCallback mLicenseCheckerCallback;
private LicenseChecker mChecker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLicenseCheckerCallback = new MyLicenseCheckerCallback(); // Construct the LicenseChecker with a Policy.
mChecker = new LicenseChecker( this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY // Your public licensing key. );
//DOSomething
}}
上面代碼中BASE64_PUBLIC_KEY是你從Market賬戶發布頁面上獲取的公鑰我們定義 private static final String BASE64_PUBLIC_KEY = "CWJdfeksdfadf....."; ,上面的SALT為20個隨機的字節,比如我們任意選20個字節樣本來做RSA運算
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 };
最後有關檢測的過程,我們可以這樣寫
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doCheck();
}
private void doCheck()
{
mCheckLicenseButton.setEnabled(false);
setProgressBarIndeterminateVisibility(true);
mStatusText.setText(R.string.checking_license);
mChecker.checkAccess(mLicenseCheckerCallback);
}
最後做清理,我們重寫onDestory方法在Activity中
@Override
protected void onDestroy() {
super.onDestroy();
mChecker.onDestroy();
}
現在來看Android Market的許可服務看上去復雜,其實如果使用默認策略直接加入庫引用代碼還是很簡單的,Android123推薦大家實際嘗試下,整個的代碼在SDK和AVD更新工具後SDK根目錄會有LVL的完整的samples代碼,明天android開發網繼續我們的游戲開發之旅系列教程。
還是要重申一遍,因為只是轉化,所以原J2ME的問題,我不會進行修改。所以這個游戲還是那個樣,各種不搭,非常不搭。。。因為實例需要,我在androidSprite中添加了
當我們在應用程序Launcher的桌面空白處長按觸摸時,會出現一個對話框,提示選擇要添加的桌面組件,如下圖所示選擇快捷方式後,會彈出一個對話框,顯示出了可添加快捷方式的
Google先後開始向自己的兩款旗艦手機Nexus One和Nexus S推送Android的小版本升級2.3.3。這也是官方版本Gingerbread發布近兩個月後,