Android 更新下载apk 安装apk 打开apk
Posted XRFirst
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 更新下载apk 安装apk 打开apk相关的知识,希望对你有一定的参考价值。
1、需要添加的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、activity
package com.junbo.downloadapkaudoinstall;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 下载Apk 自动安装Apk
*
* @author Administrator
*
*/
public class MainActivity extends Activity
private static final String URL_STRING = "http://gdown.baidu.com/data/wisegame/b7d7e4efd8199dea/tianyiyuedu_310.apk";
private File file;
private ProgressDialog dialog;
private HttpURLConnection connection;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
showWaitDialog(); // 弹出下载dialog
downFile(URL_STRING);// 开始下载APK
);
/**
*
* 显示更新状态,进度条
*/
public void showWaitDialog()
dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("正在下载更新,请稍候...");
dialog.show();
private Handler handler = new Handler()
@Override
public void handleMessage(Message msg)
super.handleMessage(msg);
switch (msg.what)
case 1:
installApk();// 安装apk
break;
case 2:
openApk(MainActivity.this, URL_STRING); // 打开apk
break;
;
// 接收到安装完成apk的广播
BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
System.out.println("接收到安装完成apk的广播");
Message message = handler.obtainMessage();
message.what = 2;
handler.sendMessage(message);
;
/**
* 后台在下面一个Apk 下载完成后返回下载好的文件
*
* @param httpUrl
* @return
*/
private File downFile(final String httpUrl)
new Thread(new Runnable()
@Override
public void run()
try
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
FileOutputStream fileOutputStream = null;
InputStream inputStream;
if (connection.getResponseCode() == 200)
inputStream = connection.getInputStream();
if (inputStream != null)
file = getFile(httpUrl);
delFile(file.getAbsolutePath());
fileOutputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length = 0;
int total = 0;
dialog.setMax(connection.getContentLength());
while ((length = inputStream.read(buffer)) != -1)
fileOutputStream.write(buffer, 0, length);
total += length;
dialog.setProgress(total);
fileOutputStream.close();
fileOutputStream.flush();
dialog.cancel();
dialog.dismiss();
inputStream.close();
System.out.println("已经下载完成");
// 往handler发送一条消息 更改button的text属性
Message message = handler.obtainMessage();
message.what = 1;
handler.sendMessage(message);
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
).start();
return file;
/***
* 删除临时存放的文件
*
* @param url
* 文件路径
*/
public void delFile(String url)
File myFile = new File(url);
if (myFile.exists())
myFile.delete();
/**
* 安装APK
*/
private void installApk()
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
@Override
protected void onStart()
super.onStart();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addDataScheme("package");
// 注册一个广播
registerReceiver(broadcastReceiver, intentFilter);
@Override
protected void onDestroy()
super.onDestroy();
// 解除广播
unregisterReceiver(broadcastReceiver);
/**
* 打开已经安装好的apk
*/
private void openApk(Context context, String url)
PackageManager manager = context.getPackageManager();
// 这里的是你下载好的文件路径
PackageInfo info = manager.getPackageArchiveInfo(Environment.getExternalStorageDirectory().getAbsolutePath() + getFilePath(url),
PackageManager.GET_ACTIVITIES);
if (info != null)
Intent intent = manager.getLaunchIntentForPackage(info.applicationInfo.packageName);
startActivity(intent);
/**
* 根据传过来url创建文件
*
*/
private File getFile(String url)
File files = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), getFilePath(url));
return files;
/**
* 截取出url后面的apk的文件名
*
* @param url
* @return
*/
private String getFilePath(String url)
return url.substring(url.lastIndexOf("/"), url.length());
3、 下载地址
以上是关于Android 更新下载apk 安装apk 打开apk的主要内容,如果未能解决你的问题,请参考以下文章