package com.test.up_soft;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import com.test.up_soft.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class DownloadService extends Service
{
private static final int NOTIFY_DOW_ID = 0;
private static final int NOTIFY_OK_ID = 1;
private Context mContext = this;
private boolean cancelled;
private int progress;
private NotificationManager mNotificationManager;
private Notification mNotification;
private DownloadBinder binder = new DownloadBinder();
private String pastVersion; //旧的本号
private String serverUrl = "http://192.168.1.30:800/tcm_android_web/apk/"; //服务器下载地址
private String xmlName ="versionInfo.xml"; //XML文件名
private int fileSize; //文件大小
private int readSize; //读取长度
private int downSize; //已下载大小
private File downFile; //下载的文件
private Map<String, String> versionInfo; //版本信息
public enum versionInfoField
{
filename, filetype, version, description
}
private Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case 0:
// 更新进度
RemoteViews contentView = mNotification.contentView;
contentView.setTextViewText(R.id.rate, (readSize < 0 ? 0 : readSize) + "b/s " + msg.arg1 + "%");
contentView.setProgressBar(R.id.progress, 100, msg.arg1, false);
// 更新UI
mNotificationManager.notify(NOTIFY_DOW_ID, mNotification);
break;
case 1:
mNotificationManager.cancel(NOTIFY_DOW_ID);
createNotification(NOTIFY_OK_ID);
/*打开文件进行安装*/
openFile(downFile);
break;
case 2:
mNotificationManager.cancel(NOTIFY_DOW_ID);
break;
}
};
};
private Handler handMessage = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 0:
Toast.makeText(mContext, "服务器连接失败,请稍后再试!", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(mContext, "服务器端文件不存在,下载失败!", Toast.LENGTH_SHORT).show();
break;
}
handler.sendEmptyMessage(2);
}
};
@Override
public void onCreate()
{
super.onCreate();
mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
cancelled = true;
}
@Override
public IBinder onBind(Intent intent)
{
// 返回自定义的DownloadBinder实例
return binder;
}
@Override
public void onDestroy()
{
super.onDestroy();
cancelled = true; // 取消下载线程
}
/**
* 创建通知
*/
private void createNotification(int notifyId)
{
switch(notifyId)
{
case NOTIFY_DOW_ID:
int icon = R.drawable.icon;
CharSequence tickerText = "开始下载";
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
// 放置在"正在运行"栏目中
mNotification.flags = Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.download_notification_layout);
contentView.setTextViewText(R.id.fileName, "正在下载:" + versionInfo.get(versionInfoField.filename.toString()) +
"." + versionInfo.get(versionInfoField.filetype.toString()));
// 指定个性化视图
mNotification.contentView = contentView;
Intent intent = new Intent(this, MainForm.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 指定内容意图
mNotification.contentIntent = contentIntent;
break;
case NOTIFY_OK_ID:
int icon2 = R.drawable.icon;
CharSequence tickerText2 = "下载完毕";
long when2 = System.currentTimeMillis();
mNotification = new Notification(icon2, tickerText2, when2);
//放置在"通知形"栏目中
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent contentInten2 = PendingIntent.getActivity(mContext, 0, null, 0);
mNotification.setLatestEventInfo(mContext, "下载完成", "文件已下载完毕", contentInten2);
stopSelf();//停掉服务自身
Toast.makeText(DownloadService.this, "下载完成", Toast.LENGTH_SHORT).show();
cancelled = true;
break;
}
// 最后别忘了通知一下,否则不会更新
mNotificationManager.notify(notifyId, mNotification);
}
/**
* 下载模块
*/
private void startDownload()
{
String dowUrl = serverUrl + versionInfo.get(versionInfoField.filename.toString())
+ "." + versionInfo.get(versionInfoField.filetype.toString());
//初始化数据
fileSize = 0;
readSize = 0;
downSize = 0;
progress = 0;
InputStream is = null;
FileOutputStream fos = null;
try
{
URL myURL = new URL(dowUrl); //取得URL
URLConnection conn = myURL.openConnection(); //建立联机
conn.connect();
fileSize = conn.getContentLength(); //获取文件长度
is = conn.getInputStream(); //InputStream 下载文件
if (is == null)
{
Log.d("tag","error");
throw new RuntimeException("stream is null");
}
//建立临时文件
downFile = File.createTempFile(versionInfo.get(versionInfoField.filename.toString()),
"." + versionInfo.get(versionInfoField.filetype.toString()));
//将文件写入临时盘
fos = new FileOutputStream(downFile);
byte buf[] = new byte[1024 * 1024];
while (!cancelled && (readSize = is.read(buf)) > 0)
{
fos.write(buf, 0, readSize);
downSize += readSize;
sendMessage(0);
}
if(cancelled)
{
handler.sendEmptyMessage(2);
downFile.delete();
}
else
{
handler.sendEmptyMessage(1);
}
}
catch (MalformedURLException e)
{
handMessage.sendEmptyMessage(0);
}
catch (IOException e)
{
handMessage.sendEmptyMessage(1);
}
catch (Exception e)
{
handMessage.sendEmptyMessage(0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
自动更新软件.zip (45个子文件)
自动更新软件
MyUpSoft
AndroidManifest.xml 1KB
res
layout
download_notification_layout.xml 1KB
filemgr.xml 734B
main.xml 790B
drawable-ldpi
icon.png 2KB
drawable-mdpi
icon.png 3KB
drawable-hdpi
icon.png 4KB
values
strings.xml 167B
proguard.cfg 1KB
src
com
test
up_soft
ParsingXMLElements.java 2KB
DownloadService.java 13KB
MainForm.java 5KB
bin
com
test
up_soft
MainForm$1.class 1KB
DownloadService$versionInfoField.class 1KB
DownloadService$DownloadBinder$1.class 1KB
ParsingXMLElements.class 3KB
MainForm$5.class 1KB
R.class 517B
MainForm$4.class 1KB
R$layout.class 471B
R$attr.class 334B
DownloadService$2.class 1KB
DownloadService$1.class 2KB
DownloadService.class 11KB
MainForm$3.class 3KB
MainForm.class 4KB
R$id.class 598B
MainForm$3$1.class 1KB
R$string.class 421B
DownloadService$DownloadBinder.class 4KB
MainForm$2.class 1KB
R$drawable.class 394B
classes.dex 22KB
MyUpSoft.apk 25KB
resources.ap_ 12KB
.classpath 280B
assets
default.properties 362B
.project 844B
gen
com
test
up_soft
R.java 1KB
多种XML版本与解析格式
版本2
ParsingXMLElements.java 2KB
new_apx.xml 170B
版本1
ParsingXMLElements.java 2KB
new_apx.xml 204B
apk
versionInfo.xml 286B
MyUpSoft.apk 25KB
共 45 条
- 1
xiesonglin19881010
- 粉丝: 1
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页