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


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值