您的位置:首页 > 移动开发 > Android开发

Android-----打包可执行文件并在apk调用

2013-09-26 22:59 507 查看
1.首先交叉编译可执行文件hello

参考:/article/7886408.html

2.将hello放到assets下



3.举例:

package com.test.android.exe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

private String exe_path = "data/data/com.test.android.exe/hello";
private File exe_file;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
copyBigDataToSD(exe_path);
exe_file = new File(exe_path);
exe_file.setExecutable(true, true);
execCmd(exe_path);
} catch (IOException e1) {
e1.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void execCmd(String cmd) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while (null != (line = br.readLine())) {
Log.e("########", line);
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}

}

private void copyBigDataToSD(String strOutFileName) throws IOException
{
InputStream myInput;
OutputStream myOutput = new FileOutputStream(strOutFileName);
myInput = this.getAssets().open("hello");
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
}


4.效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐