APK Expansion Files / Obb 接入介紹

APK Expansion Files / Obb 接入介紹

APK Expansion Files 是谷歌官方提供的APK分包下載策略, 依賴工程已經包含在SDK中.

官方資料


使用方法


1. 導入依賴工程

依賴工程放在SDK文件夾中/SDK/extras/google/文件夾下, 須要依賴market_apk_expansionmarket_licensing.html

說明1: market_apk_expansionmarket_licensing有依賴

說明2: 若是出現Android App crashes on Lollipop - Service Intent must be explicit: [duplicate]相似的報錯, 請參照連接修改工程代碼licensing工程BUGandroid

2. 打包OBB文件
3. 客戶端接入

OBB是經過谷歌後臺public key與包名進行資源匹配, 請確保這兩項正確.數組

  1. 繼承service, receiver網絡

    • 繼承 BroadcastReceiverapp

      用於在遊戲運行的時候檢查是否須要下載資源文件, 重寫`onReceive(Context, Intent)`方法, 示例
          
          @Override
          public void onReceive(Context context, Intent intent) {
              try {
                  DownloaderClientMarshaller.startDownloadServiceIfRequired(context,
                  intent, SampleDownloaderService.class);
              } catch (PackageManager.NameNotFoundException e) {
                  e.printStackTrace();
              }
          }
    • 繼承DownloaderServiceide

      新建一個class, 繼承`DownloaderService`, 重寫了裏面的三個方法, 
          
      * `getPublicKey()`, 須要在return中返回遊戲的`public key`.
      * `getSALT()`, 須要在return中返回SALT
          
          須要返回一個隨機字節數組, 格式以下
          
              public static final byte[] SALT = new byte[] { 1, 42, -12, -1, 54, 98, -100, -12, 43, 2, -8, -4, 9, 5, -106, -107, -33, 45, -1, 84};
      * `getAlarmReceiverClassName()`
          
          返回以前繼承`BroadcastReceiver`的className, 示例
          
              @Override
              public String getAlarmReceiverClassName() {
                  return SampleAlarmReceiver.class.getName();
                  }
  2. AndroidManifest.xml相關配置ui

    聲明權限:this

    <!-- Required to access Google Play Licensing -->
       <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
    
       <!-- Required to download files from Google Play -->
       <uses-permission android:name="android.permission.INTERNET" />
    
       <!-- Required to keep CPU alive while downloading files
       (NOT to keep screen awake) -->
       <uses-permission android:name="android.permission.WAKE_LOCK" />
    
       <!-- Required to poll the state of the network connection
       and respond to changes -->
       <uses-permission
       android:name="android.permission.ACCESS_NETWORK_STATE" />
    
       <!-- Required to check whether Wi-Fi is enabled -->
       <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    
       <!-- Required to read and write the expansion files on shared storage -->
       <uses-permission
       android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
       
    聲明service, receiver:
    
        <service android:name=".SampleDownloaderService" />
       <receiver android:name=".SampleAlarmReceiver" />
    1. 下載Activity的配置google

      • onCreate中添加初始化代碼

        示例
        @Overridecode

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        Intent notifierIntent = new Intent(this, MainActivity.class);
        notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 若是須要, 開始下載service
        int startResult =
                DownloaderClientMarshaller.startDownloadServiceIfRequired(this,
                        pendingIntent, SampleDownloaderService.class);
        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // 開始下載
            mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this,
                    SampleDownloaderService.class);
            return;
            }
        }catch(Throwable e) {
        e.printStackTrace();
        }
        }
        
* 接入接口
    
    Activity中implements`IDownloaderClient`.
    
    * `onServiceConnected()`
        
        當service鏈接的時候會調用此方法, 請添加以下邏輯, 得到service對象
        
            mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
            mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
            
    * `onDownloadStateChanged()`
        
        下載狀態發生變化的時候, 會調用此方法
        
    * `onDownloadProgress()`
        
        下載進度進行中會不斷調用此方法, 用於更新下載的狀態
 * service其餘API
 
     * `requestPauseDownload()`
         
         暫停下載
         
     * `requestContinueDownload()`
     
         繼續下載
         
     * `setDownloadFlags()`
     
         下載標識設置. 目前只提供一個標識, `IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR`, 若是設置, 玩家能夠經過移動網絡下載, 不然只能在WIFI狀態進行下載
相關文章
相關標籤/搜索