標籤:android style blog http color io os ar 使用
HU
轉載請註明,原文連結:http://www.cnblogs.com/xioapingguo/p/4037595.html
因為沒用lua指令碼寫遊戲,所以每次發布出去後,發現在bug,需要更新APK重新安裝,嚴重影響體驗,增加玩家流失率。如果使用直接更新.so檔案的話,就可以解決這個問題。
1、下載.so檔案,使用遊戲本身的資源更新方法,下載下來,到檔案的file/res目錄
.so檔案zip壓縮一下會小很多,解壓方法參考http://www.cnblogs.com/xioapingguo/p/4037323.html
2、把.so檔案拷貝到 存放目錄(根據自己喜好,不拷貝也可以),這裡我將存到file/libs/目錄中,把原來目錄file/res中的libgame.so刪除
檔案拷貝方法:
public static void copyFile(File sourceFile,File targetFile)throws IOException{ if(!targetFile.exists()){ targetFile.getParentFile().mkdirs(); targetFile.createNewFile(); } FileChannel fc1 = null; FileChannel fc2 = null; try { fc1 = new FileInputStream(sourceFile).getChannel(); fc2 = new FileOutputStream(targetFile).getChannel(); fc2.transferFrom(fc1, 0L, fc1.size()); } finally { if(fc1!=null) { fc1.close(); } if(fc2!=null) { fc2.close(); } } }
3、判斷是否要重啟遊戲(.so檔案更新後要重啟遊戲,因為下載資源是在之前的.so檔案裡執行的,如果想要不重啟遊戲,必須在java中做資源更新)
因為在小米系統中ALARM_SERVICE是不準的,所以重啟有可能是不會成功,只要提示使用者自己手動重啟了,目前沒有解決辦法。
public static boolean isNeedRestratApplication() throws IOException { String str = sContext.getFilesDir()+"/libs/libgame.so"; File updateFile = new File(sContext.getFilesDir()+"/res/libgame.so"); Log.d(TAG, "str="+str); if(updateFile.exists())//檢查是否有更新 { Log.d(TAG,"copyFile"); copyFile(updateFile,new File(str));//拷貝檔案 updateFile.delete();//刪除原來的檔案 } else { Log.d(TAG,"return"); return false; }
//開始重啟遊戲 str = Cocos2dxHelper.getCocos2dxPackageName(); Intent localIntent = new Intent("android.intent.action.MAIN"); localIntent.addCategory("android.intent.category.LAUNCHER"); localIntent.setComponent(new ComponentName(str,"org.cocos2dx.cpp.AppActivity")); localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Context localContext = Cocos2dxActivity.getContext(); PendingIntent localPendingIntent = PendingIntent.getActivity(localContext, (int)System.currentTimeMillis(), localIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager localAlarmManager = (AlarmManager)localContext.getSystemService(android.content.Context.ALARM_SERVICE); localAlarmManager.set(AlarmManager.RTC, 1000L+System.currentTimeMillis(), localPendingIntent);//延時1秒鐘 Process.sendSignal(Process.myPid(),Process.SIGNAL_QUIT); return true; }
4、修改Cocos2dxActivity.java下,讓遊戲使用下載的.so檔案運行遊戲。(cocos2d-x 3.2下,其它版本按具體情況修改)
protected void onLoadNativeLibraries() { try { ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); Bundle bundle = ai.metaData; String libName = bundle.getString("android.app.lib_name"); //System.loadLibrary(libName); File file = new File(getFilesDir().getAbsolutePath()+ "/libs/libgame.so");//下載到的.so檔案,如果不存在,則使用原來安裝時的 Log.d(TAG, "onLoadNativeLibraries =" + getFilesDir().getAbsolutePath()+ "libs/libgame.so"+" isexists="+file.exists()); if (file.exists()) { try { System.load(file.getAbsolutePath()); } catch(UnsatisfiedLinkError err) { Log.d(TAG, "onLoadNativeLibraries = fail"); } } else { System.loadLibrary(libName); } } catch (Exception e) { e.printStackTrace(); } }
cocos2dx中android下動態更新.so檔案