import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
public class Update extends BaseActivity {
public ProgressDialog pBar;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("系统更新")
.setMessage("发现新版本,请更新!")// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(Update.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar
.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile("http://url:8765/OA.apk");
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}
void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment
.getExternalStorageDirectory(), "OA.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}
void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/OA.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
}
}
分享到:
相关推荐
Android应用自动更新代码实现,完美实现代码的自动更新。
Android 自动更新,使程序自动更新,保持最新版本
安卓自动更新,静默更新,可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新 之前用的友盟更新,但是友盟最近自动跟新业务要停止服务了,所以就自己写了这套自动更新的代码 使用起来特别简单,几行...
Android实现自动更新代码,一行代码实现自动更新,最新封装开源库,简洁高效
Android自动更新源码,调试通过,可用。
Delphi XE android 更新 升级 自动 在线更新升级APP 然后自动安装的代码实现
Android利用布局文件自动生成声明,赋值代码。
android-auto-update 一个操作简单、支持断点续传、安全性能较高的安卓应用更新库 特点 步骤简单,几行代码就搞定 用户实现是否更新接口,有助于用户自定义 断点续传,断网后,当启动网络后继续下载 安全,代码操作...
本篇文章主要介绍了Android应用APP自动更新功能的代码实现,想要实现这个效果的同学可以了解一下。
一行代码让你实现APP自动更新功能
android app自动更新功能的实现
支持2.1以上的android版本,可以把里面的APK下载到真机上测试,可以做到和系统的GPS开关同步控制,源码是在LINUX下编译的,因为android2.1把修改GPS开关的代码HIDE了,所以在2.1window平台下是编译不了。但是APK直接...
应用程序自动更新源码.zip
使用Qt的JNI调用Android的java更新代码实现自动更新
可以自动生成Android本地代码。 解压后,在项目根目录下面运行genjni.sh(先把里面的genjni.jar放在一个地方,然后把genjni.sh里面的路径改下)。之后,如果有本地方法声明,就会生成本地文件和编译文件。
利用系统广播 一分钟更新一次时间,包括更新时分和日期
Android 自动更新 下载文件 APK 源代码 实例
包含 内外网更新、代码 libs 中有包含 aar 包,可 导入直接使用。直接输入网址、项目名、等 即可。方便快捷
网上找了一堆Delphi安卓APP程序自动升级方法都大多不太好用,有的需要配置WEB服务器,有的控制下载和升级管理麻烦,于是综合网上资料,根据项目需要整理了一个代码单元,可以直接调用,测试环境Delphi XE 10.3
android实现自动关机的具体方法代码.doc.docx