1.检测更新
接口获取相应的参数
apkName,下载地址 url
2.下载服务 看代码
public class DownAPKService extends Service {
private NotificationManager notificationMrg;
private Holder holder;
private String url;
private String apkName;
int flag = 0;
@Override
public void onCreate() {
super.onCreate();
notificationMrg = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
url = intent.getStringExtra("url");
apkName = intent.getStringExtra("apkName");
new Thread(new Runnable() {
@Override
public void run() {
loadAPKFile(url, ++flag);
}
}).start();
return START_STICKY;
}
public void loadAPKFile(String url, int flag) {
Intent notificationIntent = new Intent(getApplicationContext(), this.getClass());
// addflag设置跳转类型
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// 创建Notifcation对象,设置图标,提示文字
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
Notification notification = new Notification(R.mipmap.app_icon, apkName, System.currentTimeMillis());
// 出现在 “正在运行的”栏目下面
notification.flags |= Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_version);
contentView.setTextViewText(R.id.n_title, "下载:" + apkName);
contentView.setTextViewText(R.id.n_text, "当前进度:0%");
contentView.setProgressBar(R.id.n_progress, 100, 0, false);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
double m = 0.0;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
double length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
File file = null;
if (is != null) {
file = new File(Environment.getExternalStorageDirectory(), "Market_Master_Android.apk");
}
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[ 1024];
int ch = 0;
float count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
double temp = count / length;
if (temp >= m) {
m += 0.01;
int load = (int) (count * 100 / length);
// 函数调用handler发送信息
sendMsg(1, load, url, notification, flag, null);
}
}
Uri uri = Uri.fromFile(file);
sendMsg(2, 0, url, notification, 0, file);
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (Exception e) {
sendMsg(-1, 0, url, notification, 0, null);
}
}
// 状态栏视图更新
private Notification displayNotificationMessage(Notification notification, int count, int flag, String url) {
RemoteViews cView = notification.contentView;
cView.setTextViewText(R.id.n_text, "当前进度:" + count + "% ");
cView.setProgressBar(R.id.n_progress, 100, count, false);
notification.contentView = cView;
notificationMrg.notify(flag, notification);
return notification;
}
private void sendMsg(int what, int c, String url, Notification notification, int flag, File file) {
Message msg = new Message();
msg.what = what;// 用来识别发送消息的类型
holder = new Holder();
holder.flag = flag;
holder.count = c;
holder.url = url;
holder.notify = notification;
holder.file = file;
msg.obj = holder;// 消息传递的自定义对象信息
handler.sendMessage(msg);
}
// 定义一个Handler,用于处理下载线程与主线程间通讯
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
final Holder data = (Holder) msg.obj;
if (!Thread.currentThread().isInterrupted()) {
// 判断下载线程是否中断
switch (msg.what) {
case 1:
if (data.count >= 99) {
notificationMrg.cancel(data.flag);
break;
}
Notification notification = data.notify;
notification = displayNotificationMessage(notification, data.count, data.flag, data.url);
break;
case 2:
Toast.makeText(getApplicationContext(), "下载成功", Toast.LENGTH_SHORT).show();
installApk(data.file);
break;
case -1:
Toast.makeText(getApplicationContext(), "下载失败", Toast.LENGTH_SHORT).show();
break;
}
}
super.handleMessage(msg);
}
};
// apk文件安装
public void installApk(File file) {
/* Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(url, "application/vnd.android.package-archive");
startActivity(intent);*/
if(file.exists()){
Log.i("test",file.getName()+" "+file.getAbsolutePath()+" "+file.length());
PathUtil.chmod("777",file.getPath());
Intent intent = new Intent();
// 执行动作
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //没有i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这一步的话,最后安装好了,点打开,是不会打开新版本应用的。
// 执行的数据类型
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(this.getApplicationContext(),"net.mfinance.marketwatch.app.provider",file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
} else {
uri = Uri.fromFile(file);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid()); //没有android.os.Process.killProcess(android.os.Process.myPid());最后不会提示完成、打开。
}
}
public class Holder {
Notification notify;
String url;
int count;
int flag;
File file;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
网友评论