1 package ai.suanzi.rtmpclient;
3 import android.app.IntentService;
4 import org.apache.log4j.Logger;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.app.NotificationManager;
9 import android.net.Uri;
10 import android.support.v4.app.NotificationCompat;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.HttpURLConnection;
20 public class DownloadApkService extends IntentService {
22 private static final int BUFFER_SIZE = 10 * 1024;
23 private Logger gLogger = Logger.getLogger("DownloadApkService");
25 private static final int NOTIFICATION_ID = 0;
26 private NotificationManager mNotifyManager;
27 private NotificationCompat.Builder mBuilder;
31 public DownloadApkService() {
32 super("DownloadApkService");
36 protected void onHandleIntent(Intent intent) {
37 mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
38 mBuilder = new NotificationCompat.Builder(this);
39 String appName = getString(getApplicationInfo().labelRes);
40 int icon = getApplicationInfo().icon;
41 mBuilder.setContentTitle(appName).setSmallIcon(icon);
43 String urlStr = intent.getStringExtra("apkUrl");
44 gLogger.debug("onHandleIntent - " + urlStr);
45 InputStream in = null;
46 FileOutputStream out = null;
48 URL url = new URL(urlStr);
49 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
50 urlConnection.setRequestMethod("GET");
51 urlConnection.setDoOutput(false);
52 urlConnection.setConnectTimeout(10 * 1000);
53 urlConnection.setReadTimeout(10 * 1000);
54 urlConnection.setRequestProperty("Connection", "Keep-Alive");
55 urlConnection.setRequestProperty("Charset", "UTF-8");
56 urlConnection.setRequestProperty("Accept-Encoding", "gzip, defalte");
57 urlConnection.connect();
59 long bytetotal = urlConnection.getContentLength();
62 in = urlConnection.getInputStream();
63 File dir = this.getExternalCacheDir();
64 String apkName = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length());
65 File apkFile = new File(dir, apkName);
66 out = new FileOutputStream(apkFile);
67 byte[] buffer = new byte[BUFFER_SIZE];
71 while((byteread = in.read(buffer)) != -1){
73 out.write(buffer, 0, byteread);
74 int progress = (int) (bytesum * 100L / bytetotal);
75 if(progress != oldProgress){
76 updateProgress(progress);
78 oldProgress = progress;
81 gLogger.debug("onHandleIntent, download apk finish");
82 mNotifyManager.cancel(NOTIFICATION_ID);
83 } catch (Exception e){
84 gLogger.error("download apk file error, " + e.getMessage());
90 } catch (IOException e){
91 gLogger.error(e.getMessage());
97 } catch (IOException e){
98 gLogger.error(e.getMessage());
104 private void updateProgress(int progress){
105 if((progress % 5) == 0)
106 gLogger.debug("updateProgress - " + progress + "%");
109 private void installApk(File apkFile) {
111 gLogger.debug("installApk - " + apkFile.toString());
112 Intent intent = new Intent(Intent.ACTION_VIEW);
114 // String[] command = { "chmod", "777", apkFile.toString() };
115 // ProcessBuilder builder = new ProcessBuilder(command);
116 // gLogger.error("installApk - start");
118 // } catch (IOException e){
119 // gLogger.error(e.getMessage());
120 // e.printStackTrace();
122 intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
123 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // "application/vnd.android.package-archive"
124 startActivity(intent);