public class UpdateService extends Service { private int count =0 ; private ProgressBar bar; private Button btnC; private RemoteViews rv; Notification notification; NotificationManager mManager; /* 下载保存路径 */ private String mSavePath; /* 保存解析的XML信息 */ HashMap<String, String> mHashMap; private boolean notUpdating = false; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Notification notification = new Notification.Builder(this) // .setContentTitle("New mail from " + "linsenxxxx") // .setContentText("setContentText setContentText setContentText ") // .setSmallIcon(R.drawable.tag02) // .build(); notification = new Notification(R.drawable.ic_launcher, "正在下载更新包", System.currentTimeMillis()); } Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0x111: rv.setProgressBar(R.id.update_progressBar, 100, count, false); notification.contentView = rv; mManager.notify(0, notification); break; case 0x222: // 安装文件 installApk(); mManager.cancelAll(); break; case 0x333: showToast(); break; } } }; @Override public int onStartCommand(Intent intent, int flags, int startId) { /* 获取intent */ // Intent it = new Intent(this, UpdateActivity.class); Bundle bundle = (Bundle) intent.getExtras(); if (bundle != null) { mHashMap = (HashMap<String, String>) bundle.getSerializable("HashMap"); Log.v("0627", "hashmap" + mHashMap.get("url")); } PendingIntent contentIntent = PendingIntent.getActivity(this, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(this, "Hello,there!", "Hello,there,I'm john.", contentIntent); rv = new RemoteViews(this.getPackageName(), R.layout.update_progress_bar); notification.contentView = rv; // 开启任务的通知 mManager.notify(0, notification); // 开启下载任务 new DownloadApkThread().start(); // new Thread() { // public void run() { // // // for (int i = 0; i < 100; i++) { // try { // sleep(1000); // } catch (Exception e) { // Log.v("0627", "error"); // } // Log.v("0627", String.valueOf(++count)); // count = i; // handler.sendEmptyMessage(0x111); // } // } // }.start(); return 0; } private class DownloadApkThread extends Thread { @Override public void run() { try { // 判断SD卡是否存在,并且是否具有读写权限 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 获得存储卡的路径 String sdpath = Environment.getExternalStorageDirectory() + "/"; mSavePath = sdpath + "download"; URL url = new URL(mHashMap.get("url")); // 创建连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); // 获取文件大小 int length = conn.getContentLength(); // 创建输入流 InputStream is = conn.getInputStream(); File file = new File(mSavePath); // 判断文件目录是否存在 if (!file.exists()) { file.mkdir(); } File apkFile = new File(mSavePath, mHashMap.get("name")); FileOutputStream fos = new FileOutputStream(apkFile); int counttemp = 0; // 缓存 byte buf[] = new byte[1024]; // 写入到文件中 do { int numread = is.read(buf); counttemp += numread; // 计算进度条位置 int countTemp = (int) (((float) counttemp / length) * 100); if(count<countTemp){ count = countTemp; mHandler.sendEmptyMessage(0x111); } Log.v("0627","countTEMP==="+String.valueOf(countTemp)); Log.v("0627","count==="+String.valueOf(count)); // 更新进度 if (numread <= 0) { // 下载完成 mHandler.sendEmptyMessage(0x222); break; } // 写入文件 fos.write(buf, 0, numread); } while (!notUpdating);// 点击取消就停止下载. fos.close(); is.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 取消下载对话框显示 // mDownloadDialog.dismiss(); } }; public void showToast() { Dialog d = new AlertDialog.Builder(this).setTitle(R.string.soft_update_title).setMessage(R.string.sdcard_no) .setNegativeButton("知道了!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }).create(); d.show(); } /** * 安装APK文件 */ private void installApk() { File apkfile = new File(mSavePath, mHashMap.get("name")); if (!apkfile.exists()) { return; } // 通过Intent安装APK文件 Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } }