使用EgretNativeAndroid对游戏热更新
Posted 汤米粥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用EgretNativeAndroid对游戏热更新相关的知识,希望对你有一定的参考价值。
LaunchActivity.java
package org.egret.testUpdate;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class LaunchActivity extends Activity
private Button btn_load;
private Button btn_game;
// private final String gameUrl = "http://game.com/game/index.html";
// private final String zipUrl = "http://tool.egret-labs.org/Weiduan/game/game2.zip";
private final String gameUrl = Define.GAME_URL;
private final String zipUrl = Define.ZIP_URL;
private final String preloadPath = "/sdcard/egretGame/";
private static String[] permissions = Manifest.permission.WRITE_EXTERNAL_STORAGE;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
btn_load = (Button) findViewById(R.id.btn_load);
btn_game = (Button) findViewById(R.id.btn_game);
btn_load.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
btn_load.setEnabled(false);
btn_game.setEnabled(false);
preloadGame();
);
btn_game.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
intent.putExtra("preloadPath", preloadPath);
startActivity(intent);
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
int check = checkSelfPermission(permissions[0]);
if (check != PackageManager.PERMISSION_GRANTED)
requestPermissions(permissions, 111);
private void preloadGame()
String dir = preloadPath + getFileDirByUrl(gameUrl);
File dirFile = new File(dir);
if (!dirFile.exists())
dirFile.mkdirs();
downloadGameRes(zipUrl, dir);
private void downloadGameRes(final String zipUrl, String targetDir)
String tempZipFileName = targetDir + "game.zip";
final File file = new File(tempZipFileName);
Runnable runnable = new Runnable()
@Override
public void run()
InputStream inputStream = null;
FileOutputStream outputStream = null;
HttpURLConnection connection = null;
boolean finish = false;
try
URL url = new URL(zipUrl);
connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
if (code == 200)
inputStream = connection.getInputStream();
outputStream = new FileOutputStream(file, true);
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, length);
finish = true;
catch (Exception e)
e.printStackTrace();
finally
try
if (outputStream != null)
outputStream.close();
if (inputStream != null)
inputStream.close();
if (connection != null)
connection.disconnect();
catch (Exception e)
e.printStackTrace();
return;
if (finish)
unzip(file);
;
new Thread(runnable).start();
private void unzip(File file)
int BUFFER = 4096;
String strEntry;
String targetDir = file.getParent() + "/";
try
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null)
try
int count;
byte data[] = new byte[BUFFER];
strEntry = entry.getName();
File entryFile = new File(targetDir + strEntry);
if (strEntry.endsWith("/"))
entryFile.mkdirs();
continue;
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists())
entryDir.mkdirs();
FileOutputStream fos = new FileOutputStream(entryFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1)
dest.write(data, 0, count);
dest.flush();
dest.close();
catch (Exception ex)
ex.printStackTrace();
return;
zis.close();
catch (Exception e)
e.printStackTrace();
return;
file.delete();
runOnUiThread(new Runnable()
@Override
public void run()
btn_game.setEnabled(true);
);
private static String getFileDirByUrl(String urlString)
int lastSlash = urlString.lastIndexOf('/');
String server = urlString.substring(0, lastSlash + 1);
return server.replaceFirst("://", "/").replace(":", "#0A");
MainActivity.java
package org.egret.testUpdate;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import org.egret.runtime.launcherInterface.INativePlayer;
import org.egret.egretnativeandroid.EgretNativeAndroid;
public class MainActivity extends Activity
private final String TAG = "MainActivity";
private EgretNativeAndroid nativeAndroid;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
nativeAndroid = new EgretNativeAndroid(this);
if (!nativeAndroid.checkGlEsVersion())
Toast.makeText(this, "This device does not support OpenGL ES 2.0.",
Toast.LENGTH_LONG).show();
return;
nativeAndroid.config.showFPS = true;
nativeAndroid.config.fpsLogTime = 30;
nativeAndroid.config.disableNativeRender = false;
nativeAndroid.config.clearCache = false;
nativeAndroid.config.loadingTimeout = 0;
Intent intent = getIntent();
nativeAndroid.config.preloadPath = intent.getStringExtra("preloadPath");
setExternalInterfaces();
if (!nativeAndroid.initialize(Define.GAME_URL))
Toast.makeText(this, "Initialize native failed.",
Toast.LENGTH_LONG).show();
return;
setContentView(nativeAndroid.getRootFrameLayout());
@Override
protected void onPause()
super.onPause();
nativeAndroid.pause();
@Override
protected void onResume()
super.onResume();
nativeAndroid.resume();
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent)
if (keyCode == KeyEvent.KEYCODE_BACK)
nativeAndroid.exitGame();
return super.onKeyDown(keyCode, keyEvent);
private void setExternalInterfaces()
nativeAndroid.setExternalInterface("sendToNative", new INativePlayer.INativeInterface()
@Override
public void callback(String message)
String str = "Native get message: ";
str += message;
Log.d(TAG, str);
nativeAndroid.callExternalInterface("sendToJS", str);
);
@Override
protected void onDestroy()
super.onDestroy();
Define.java
package org.egret.testUpdate;
public class Define
// public static final String GAME_URL = "http://game.com/game/index.html";
public static final String GAME_URL = "http://192.168.1.170:8080/fish_game/index.html";
public static final String ZIP_URL = "http://192.168.1.170:8080/game1/fish_game.zip";
原文链接:https://blog.csdn.net/wulong710/article/details/89096256
以上是关于使用EgretNativeAndroid对游戏热更新的主要内容,如果未能解决你的问题,请参考以下文章