当前位置: 代码迷 >> Android >> Android-封装可执行文件并在apk调用
  详细解决方案

Android-封装可执行文件并在apk调用

热度:6   发布时间:2016-04-28 07:57:15.0
Android-----打包可执行文件并在apk调用

1.首先交叉编译可执行文件hello

参考:http://blog.csdn.net/yf210yf/article/details/9009367


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.效果:


  相关解决方案