public class TfkDownLoadService extends Service {//下载新版本服务
private int NOTIFY_ID = 0;
public static boolean isrunningLoadingActivity = false;
public static boolean done = false;
private Context mContext = this;
private NotificationManager mNotificationManager;
private Notification mNotification;
String url = "";
String path = "";
String name = "";
Resources r;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (!Thread.currentThread().isInterrupted()) {
Intent intent = null;
switch (msg.what) {
case 0:
break;
case 1: // 不用通知
break;
case 2:// 未在loading 通知下载完成
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
mNotification = new Notification(icon, r
.getString(R.string.update_title), when);
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(
Tfk.downLoadPath + name)),
"application/vnd.android.package-archive");
PendingIntent contentIntent = PendingIntent.getActivity(
mContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mNotification.setLatestEventInfo(mContext, r
.getString(R.string.update_title), r
.getString(R.string.update_down_done), contentIntent);
mNotificationManager.notify(NOTIFY_ID, mNotification);
break;
default:
break;
}
stopSelf();
}
}
};
@Override
public void onCreate() {
super.onCreate();
mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
r = getResources();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(Tfk.tag, "name:" + intent.getStringExtra("name"));
Log.d(Tfk.tag, "url:" + intent.getStringExtra("url"));
Log.d(Tfk.tag, "path:" + intent.getStringExtra("path"));
super.onStart(intent, startId);
name = intent.getStringExtra("name");
url = intent.getStringExtra("url");
path = intent.getStringExtra("path");
new Thread() {
public void run() {
try {
startDownload(url, path, name);
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
private void startDownload(String url, String path, String filename)
throws IOException {
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int fileSize = 0;
int downLoadSize = 0;
fileSize = conn.getContentLength();
System.out.println("fileSize:"+fileSize);
//fileSize =
if (fileSize <= 0)
throw new RuntimeException("can't get file size");
if (is == null)
throw new RuntimeException("stream is null");
FileOutputStream fos = new FileOutputStream(path + filename);
byte buf[] = new byte[1024];
// downLoadSize = 0;
int downloadCount = 0;
int rate = 0;
Intent intent = null;
do {
int numread = is.read(buf);
if (numread == -1) {
break;
}
fos.write(buf, 0, numread);
downLoadSize += numread;
rate = (int) ((double) downLoadSize / (double) fileSize * 100);
if ((downloadCount == 0) || rate > downloadCount) {
downloadCount += 1;
intent = new Intent();
intent.setAction("android.intent.action.MY_RECEIVER");
intent.putExtra("progress", rate);
sendBroadcast(intent);
// sendMsg(1, rate);
if (rate >= 100) {
if (!isrunningLoadingActivity) {
sendMsg(2, rate);
} else {
sendMsg(1, rate);
}
}
}
} while (true);
// sendMsg(2, rate);
try {
is.close();
done = true;
} catch (Exception ex) {
Log.e("tag", "error: " + ex.getMessage(), ex);
}
}
private void sendMsg(int flag, int rate) {
Message msg = new Message();
msg.what = flag;
msg.arg1 = rate;
handler.sendMessage(msg);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
下面是activity中确认下载后的工作
private void doDownLoad(String url1, String path1) {
// 创建ProgressDialog对象
tfkDialog = new ProgressDialog(LoadingActivity.this);
// 设置进度条风格
tfkDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 标题
tfkDialog.setTitle(resources.getString(R.string.update_down_title));
// 设置ProgressDialog提示信息
tfkDialog.setMessage(resources.getString(R.string.update_downloading));
// // 设置ProgressDialog标题图标
// tfkDialog.setIcon(R.drawable.img2);
// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
tfkDialog.setIndeterminate(false);
// 设置ProgressDialog 进度条进度
tfkDialog.setProgress(100);
// 设置ProgressDialog 是否可以按退回键取消
tfkDialog.setCancelable(false);
// 让ProgressDialog显示
tfkDialog.show();
url = url1;
path = path1;
url = sr.getVersionCheckReponse().getAppUrl();
filename = url.substring(url.lastIndexOf("/") + 1);
Intent intent = new Intent();
intent.setClass(LoadingActivity.this, TfkDownLoadService.class);
intent.putExtra("name", filename);
intent.putExtra("path", Tfk.downLoadPath);
intent.putExtra("url", url);
LoadingActivity.this.startService(intent);
}