提取apk文件 (四) 大結局


前三篇

先上成果。

手機=》web服務=》什么服務=》提供下載手機里的apk文件

=》干什么用=》從市場下載應用供apk上傳到電腦的問題=》反編譯它用來學習。

 

ok,先回到應用本身。

上篇我們完成了一個簡單的web服務器。

然后我們在Activity里用一個線程啟動服務器。注意在權限里增加internet權限。因為這個耽誤了半個小時。嗚嗚。

// TODO 使用一個線程來啟動服務器
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
new HttpServer().start(8000);
Log.v(tag, "http server start");

}
}).start();


然后根據需要提供列出所有應用,以及文件下載的兩個http Handler。

public class DefaultHandler implements IHandler{
public String makeList(){
String body="";
List<ResolveInfo> apps = ApkActivity.apps;
PackageManager pm = ApkActivity.pm;
String path;
if(apps!=null&&apps.size()>0){
for(ResolveInfo app:apps){
path=app.activityInfo.applicationInfo.sourceDir;
body+="<p><a href='"+path+"'>"+app.loadLabel(pm)+"</a></p>";
}
}
return body;

}
public void doResponse(DataOutputStream out) throws IOException{
out.writeBytes("HTTP/1.0 200 \r\n");
out.writeBytes("Content-Type: text/html \r\n");
out.writeBytes("\r\n");
String head="<meta http-equiv=Content-Type content=text/html; charset=UTF-8>";
out.writeBytes(head);
out.writeBytes("<h1>Your Apps</h1>");
out.write(makeList().getBytes());
out.flush();
}
}

默認的首頁,相當於index。列出所以app。這里的代碼質量不太好。先這樣,出個alpha版本再說。

public class FileHandler implements IHandler{
private File file;
public FileHandler(File f) {
file=f;
// TODO Auto-generated constructor stub
}
public void doResponse(DataOutputStream out) throws IOException{
if(file.exists()){
out.writeBytes("HTTP/1.0 200 OK\r\n");
out.writeBytes("Content-Type: application/octet-stream \r\n");
out.writeBytes("\r\n");
FileInputStream fis = new FileInputStream(file);
byte buf[]=new byte[1024];
int r;
while((r=fis.read(buf))!=-1){
out.write(buf);
}
}else{
out.writeBytes("HTTP/1.0 404 File Not Found\r\n");
out.writeBytes("\r\n");
}
out.flush();
}
}

文件下載的handler。就是根據文件的路徑下載咯。linux文件系統沒有盤符的考慮,這點處理起來比較方便。

    private void doResponse() {
// TODO Auto-generated method stub
//先做簡單的處理
if(query!=null){
try {
OutputStream out = waiter.getOutputStream();
DataOutputStream dos=new DataOutputStream(out);
if(query.equals("/")){
new DefaultHandler().doResponse(dos);
}else if(query.endsWith(".apk")){
new FileHandler(new File(query)).doResponse(dos);
}
waiter.shutdownOutput();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

經過改造的doResponse方法。本來想實現urlmap,不過沒經驗,先這樣將就吧。哈哈。

 

到此為止,0.1版本的,嗯,想個名字,apk getter,就完成了。

 

 

 

@我是函數,出品,版權所有,轉載請注明出處。

需要代碼的請留下郵箱,代碼雖爛,情誼在。

歡迎交流。

 



 》》》注意

文件輸出用DataOutputStream存在錯誤,已經改用原始的outputstream。



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM