服务器 远程安装zip,从远程服务器安装apk文件

这是我使用的代码,它是不是为web视图,但你可以轻松地覆盖url加载和反向应用此代码.. 底部的意图是你的问题的答案。

/**

* Main

* When started, will download latest version of AN APPLICATIONand launch an install

* Is just a dialog

*

* REQUIRES SDCARD

* @author Dag

*

*/

public class Main extends Activity {

ProgressDialog dlDialog;

String path = Environment.getExternalStorageDirectory()+ "/"; // Path to where you want to save the file

String inet = "http://www.google.com/test.apk"; // Internet path to the file

String cachedir = "";

String filename = "TMC.apk";

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView webview = new TextView(this);

setContentView(webview);

File getcache = this.getCacheDir();

cachedir = getcache.getAbsolutePath();

dlDialog = new ProgressDialog(Main.this);

dlDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

dlDialog.setTitle("Downloadin");

dlDialog.setMessage("Connecting");

dlDialog.show();

new Thread(new Runnable() {

public void run() {

String filePath = path;

InputStream is = null;

OutputStream os = null;

URLConnection URLConn = null;

try {

URL fileUrl;

byte[] buf;

int ByteRead = 0;

int ByteWritten = 0;

fileUrl = new URL(inet);

URLConn = fileUrl.openConnection();

is = URLConn.getInputStream();

String fileName = inet.substring(inet.lastIndexOf("/") + 1);

File f = new File(filePath);

f.mkdirs();

String abs = filePath + fileName;

f = new File(abs);

os = new BufferedOutputStream(new FileOutputStream(abs));

buf = new byte[1024];

/*

* This loop reads the bytes and updates a progressdialog

*/

while ((ByteRead = is.read(buf)) != -1) {

os.write(buf, 0, ByteRead);

ByteWritten += ByteRead;

final int tmpWritten = ByteWritten;

runOnUiThread(new Runnable() {

public void run() {

dlDialog.setMessage(""+tmpWritten+" Bytes");

}

});

}

runOnUiThread(new Runnable() {

public void run() {

dlDialog.setTitle("Startar");

}

});

is.close();

os.flush();

os.close();

Thread.sleep(200);

dlDialog.dismiss();

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(new File(abs)),

"application/vnd.android.package-archive");

startActivity(intent);

finish();

} catch (Exception e) {

e.printStackTrace();

}

}

}).start();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值