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

来源:互联网 发布:java 网络通讯框架 编辑:程序博客网 时间:2024/05/21 07:57

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

package com.example.helle;



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;
import android.widget.TextView;  
  
public class MainActivity extends Activity {  
  
    private String exe_path = "data/data/com.example.helle/hello";  
    private File exe_file;  
    private TextView txt;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main); 
        
        txt = (TextView) findViewById(R.id.text);
        Log.d("vane", "1");
        try {  
            copyBigDataToSD(exe_path);  
            Log.d("vane", "2");
            exe_file = new File(exe_path);    
            exe_file.setExecutable(true, true);  
            Log.d("vane", "3");
            execCmd(exe_path); 
           
            Log.d("vane", "4");
        } 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;  
    }  
      
  
public void execCmd(String cmd) throws IOException {
long startTime = System.currentTimeMillis();
Log.d("vane", "5");
    Runtime runtime = Runtime.getRuntime();  
    Process process = runtime.exec(cmd);  
    Log.d("vane", "6");
    InputStream is = process.getInputStream();  
    InputStreamReader isr = new InputStreamReader(is);  
    BufferedReader br = new BufferedReader(isr);  
    Log.d("vane", "7");
    long endTime = System.currentTimeMillis();
    System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
    long time = (endTime - startTime);
    String a = String.valueOf(time); 
    txt.setText(a+"ms");
    Log.d("vane", cmd + time);
    String line = null;  
    while (null != (line = br.readLine())) {  
    Log.d("vane", "8");
        Log.e("########", line);  
    }       
    try {  
        process.waitFor();  
    } catch (InterruptedException e) {  
        e.printStackTrace();  
    }  
  
}  
  
private void copyBigDataToSD(String strOutFileName) throws IOException   
{    Log.d("vane", "9");
    InputStream myInput;    
    OutputStream myOutput = new FileOutputStream(strOutFileName);  
    Log.d("vane", "10");
    myInput = this.getAssets().open("hello");  
    Log.d("vane", "11");
    byte[] buffer = new byte[1024];    
    int length = myInput.read(buffer);  
    while(length > 0)  
    {  Log.d("vane", "12");
        myOutput.write(buffer, 0, length);   
        length = myInput.read(buffer);  
        Log.d("vane", "13");
    }  
        myOutput.flush();    
        myInput.close();    
        myOutput.close();          
    }  
}  
0 0
原创粉丝点击