前面已经得出android中加载本地html5所遇到问题的原因,针对这个原因,目前有以下两种解决方案,第一种解决方案:
对于每一个景区,新建一个android application,然后将动景手动复制到工程的asset中,然后用webView从asset中加载动景,这样就避免了webkit内核禁止加载本地html5的问题,然后将应用打包成apk,这样每次都下载景区对应的动景apk,下载完之后自动安装,这样只需要访问apk就可以解决了,安装apk:
public
static
boolean
install(Context context, String filePath) {
Intent i =
new
Intent(Intent.
ACTION_VIEW
);
File file =
new
File(filePath);
if
(file !=
null
&& file.length() > 0 && file.exists() && file.isFile()) {
i.setDataAndType(Uri.parse(
"file://"
+ filePath),
"application/vnd.android.package-archive"
);
i.addFlags(Intent.
FLAG_ACTIVITY_NEW_TASK
);
context.startActivity( i);
return
true
;
}
return
false
;
}
安装完之后,我们只需要根据apk的包名去访问apk,代码如下:
Intent intent =
new
Intent(Intent.
ACTION_MAIN
);
intent.addCategory(Intent.
CATEGORY_LAUNCHER
);
ComponentName cn =
new
ComponentName(
"com.ly"
,
"com.ly.MainActivity"
);
intent.setComponent(cn);
startActivity(intent);
其中
new
ComponentName(
"com.ly"
,
"com.ly.MainActivity"
)中所传的第一个参数是要访问的apk的包名,第二个参数是要访问的apk的入口的activity的类名(包括包名)。
接着,我们来介绍第二种解决方案,既然weblit内核的浏览器可以调用服务器的html5,那我们为何不在本地写一个服务器,然后通过访问本地的服务器去加载本地的html5,这样就不存在安全性设置的问题了。在这里,我们在服务器端先采用单线程去处理客户端发送过来的请求。下面是服务器的源码:
public
class
HttpServer
implements
Runnable {
static
HttpServer
httpServer
;
private
static
final
String
TAG
=
"HttpServer"
;
/**
* 服务器端的socket
*/
ServerSocket
server
;
/**
* 服务器监听的端口号
*/
public
static
final
int
PORT
= 8080;
// public static HttpServer getInstance(){
// if(httpServer == null){
// httpServer = new HttpServer();
// }
// return httpServer;
// }
public
HttpServer() {
try
{
server
=
new
ServerSocket(
PORT
);
}
catch
(IOException e) {
//
TODO
Auto-generated catch block
Log. i(
TAG
,
"异常信息:"
+ e.getMessage());
}
new
Thread(
this
).start();
Log. i(
TAG
,
"server is start......"
);
}
public
void
run() {
// 服务器端连续监听客户端
while
(
true
) {
Socket client =
null
;
try
{
client =
server
.accept();
// 创建分线程
// httpRequestHandler request = new httpRequestHandler(client);
// // 启动线程
// new Thread(request).start();
if
(client!=
null
){
Log. i(
TAG
,
"已连接到服务器:"
+client);
//获取客户端的输入流
BufferedReader in =
new
BufferedReader(
new
InputStreamReader(client.getInputStream()));
//读取第一行
String line = in.readLine();
if
(line==
null
||line.equals(
""
)||line.equals(
"\r\n"
)){
break
;
}
Log. i(
TAG
,
"客户端发送的消息是:"
+line);
String[] tokens = line.split(
" "
);
if
(tokens[0].equalsIgnoreCase(
"GET"
)){
String fileName = tokens[1];
fileService(fileName, client);
}
}
}
catch
(IOException e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 读取文件的内容
*
*
@param
fileName
*
@param
socket
*/
public
void
fileService(String fileName, Socket socket) {
PrintStream os =
null
;
try
{
os =
new
PrintStream(socket.getOutputStream(),
true
);
File file =
new
File(fileName);
if
(file.exists() && !file.isDirectory()) {
os.println(
" HTTP/1.0 200 OK "
);
// 返回应答消息,并结束应答
os.println(
" Content-Type:"
+ generatecontentType(fileName));
os.println(
" Content-Length: "
+ file.length());
// 返回内容字节数
os.println();
FileInputStream fis =
null
;
try
{
fis =
new
FileInputStream(file);
}
catch
(FileNotFoundException e) {
//
TODO
Auto-generated catch block
os.println(
"HTTP/1.0 404"
+ e.getMessage());
}
byte
[] buffer =
null
;
Log. i(
TAG
,
"文件大小:"
+ buffer.
length
);
Log. i(
TAG
,
"内容:"
+ buffer.toString());
buffer =
new
byte
[fis.available()];
fis.read(buffer);
os.write(buffer);
os.flush();
os.close();
fis.close();
socket.close();
}
else
{
os.println(
"HTTP/1.0 404 FileNotFonud!"
);
os.close();
}
}
catch
(IOException e) {
//
TODO
Auto-generated catch block
os.println(
"HTTP/1.0 404"
+ e.getMessage());
}
}
private
String generatecontentType(String fileName) {
if
(fileName.endsWith(
"html"
) || fileName.endsWith(
"htm"
)
|| fileName.endsWith(
"xml"
)) {
return
"text/html"
;
}
else
if
(fileName.endsWith(
"png"
)) {
return
"application/binarary"
;
}
else
if
(fileName.endsWith(
"jpg"
)) {
return
"image/jpeg"
;
}
else
if
(fileName.endsWith(
"js"
)) {
return
"application/x-javascript"
;
}
else
if
(fileName.endsWith(
"swf"
)) {
return
"application/x-shockwave-flash"
;
}
return
"*/*"
;
}
}