Android 更新下载apk 安装apk 打开apk
2015-01-29 17:55
791 查看
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应用检查更新下载安装打开
- Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题
- Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题
- Android下载完成更新后,没有打开安装成功界面,出现闪退问题
- Android 下载APK 安装APK 打开APK
- Android----- 版本更新和 服务器下载新版本APK并安装
- Android 文件断点下载和通知栏的提示及apk更新安装
- Android----- 版本更新和 服务器下载新版本APK并安装
- Android 在线下载更新App 下载完成安装APK(兼容Android7.0)
- Android 下载APK 安装APK 打开APK
- Android apk下载 安装 卸载 打开
- Android下载完成更新后,闪退,并没有打开安装成功界面
- Android 下载APK 安装APK 打开APK
- 自动更新下载完APK时,提示选择打开方式不直接安装
- Android中检查更新APK,以及下载APK安装。
- Android客户端apk自动检测更新自动下载自动安装的实现方法
- 下载APK后打开APK,在安装时的报错 android.os.FileUriExposedException:exposed beyond app through Intent.getData()
- Android----- 版本更新和 服务器下载新版本APK并安装
- android 7.0 apk版本更新,下载之后自动安装
- android的APP自动更新程序,检测版本,然后下载安装,但app安装后不提示“完成,打开”?