public class DownloadService extends IntentService { private static final int BUFFER_SIZE = 10 * 1024; private static final String TAG = "DownloadService"; private static final int NOTIFICATION_ID = 0; private NotificationManager mNotifyManager; private Builder mBuilder; public DownloadService() { super("DownloadService"); } @Override protected void onHandleIntent(Intent intent) { mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new Builder(this); String appName = getString(getApplicationInfo().labelRes); int icon = getApplicationInfo().icon; mBuilder.setContentTitle(appName).setSmallIcon(icon); String urlStr = intent.getStringExtra("downloadUrl"); InputStream in = null; FileOutputStream out = null; try { URL url = new URL(urlStr); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(false); urlConnection.setConnectTimeout(10 * 1000); urlConnection.setReadTimeout(10 * 1000); urlConnection.setRequestProperty("Connection", "Keep-Alive"); urlConnection.setRequestProperty("Charset", "UTF-8"); urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate"); urlConnection.connect(); long bytetotal = urlConnection.getContentLength(); long bytesum = 0; int byteread = 0; in = urlConnection.getInputStream(); File dir = StorageUtils.getCacheDirectory(this); String apkName = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length()); File apkFile = new File(dir, apkName); out = new FileOutputStream(apkFile); byte[] buffer = new byte[BUFFER_SIZE]; int oldProgress = 0; while ((byteread = in.read(buffer)) != -1) { bytesum += byteread; out.write(buffer, 0, byteread); int progress = (int) (bytesum * 100L / bytetotal); if (progress != oldProgress) { updateProgress(progress); } oldProgress = progress; } installAPk(apkFile); AppConfig.IsCheck = false; mNotifyManager.cancel(NOTIFICATION_ID); } catch (Exception e) { Log.e(TAG, "download apk file error"); AppConfig.IsCheck = false; } finally { if (out != null) { try { out.close(); } catch (IOException ignored) { } } if (in != null) { try { in.close(); } catch (IOException ignored) { } } } } private void updateProgress(int progress) { mBuilder.setContentText("正在下载:" + progress + "%").setProgress(100, progress, false); PendingIntent pendingintent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT); mBuilder.setContentIntent(pendingintent); mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build()); } // private void installAPk(File apkFile) { // Intent intent = new Intent(Intent.ACTION_VIEW); // try { // String[] command = {"chmod", "777", apkFile.toString()}; // ProcessBuilder builder = new ProcessBuilder(command); // builder.start(); // } catch (IOException ignored) { // } // intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); // // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // startActivity(intent); // // //普通安装 private void installAPk(File apkPath) { Intent intent = new Intent(Intent.ACTION_VIEW); // 由于没有在Activity环境下启动Activity,设置下面的标签 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { String[] command = {"chmod", "777", apkPath.toString()}; ProcessBuilder builder = new ProcessBuilder(command); builder.start(); } catch (IOException ignored) { ignored.printStackTrace(); } //版本在7.0以上是不能直接通过uri访问的 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // File file = (new File(apkPath)); // // 由于没有在Activity环境下启动Activity,设置下面的标签 // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件 Uri apkUri = FileProvider.getUriForFile(this, "com.spark.huabang", apkPath); // 加这一句表示对目标应用临时授权该Uri所代表的文件 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(apkPath), "application/vnd.android.package-archive"); } startActivity(intent); } }
下载apk的services封装
最新推荐文章于 2021-05-26 04:36:14 发布