前面已经得出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 "*/*" ;
}
}
相关推荐
android studio加载本地的html的文件
android webView加载html 并引用本地资源(图片、字体库)
android webview 加载本地html.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
http://blog.csdn.net/menxu_work/article/details/12947837
android中使用webview嵌入本地html,支持ajax请求,支持css,js加载
该资源包含了一个android通过html打开本地app并且可以传递数据,简单易懂,喜欢的下载。
本程序使用webview控件,加载本地写的Html文件,操作本地数据。包含js调用java方法,以及java调用js方法,实现数据的传递。
android webview 加载本地html 实现 与 java 之间的相互响应.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
demo中介绍使用webview加载本地页面,并在java代码中提供js调用事件,在html中动态生成组件button
/*通过Stream重新加载图片(一般用于网络直接下载显示) drawable = Drawable.createFromStream((new URL(source)... 通过Path重新加载图片(一般用于手机本地加载图片) drawable = Drawable.createFromPath(uri);*/
Android设备多分辨率的问题 Android浏览器默认预览模式浏览 会缩小页面 WebView中则会以原始大小显示 Android浏览器和WebView默认为mdpi。hdpi相当于mdpi的1.5倍 ldpi相当于0.75倍 三种解决方式:1 viewport属性 2 ...
1、文章:https://kunnan.blog.csdn.net/article/details/114015528 2、应用场景:使用原生视图UILabel显示服务端返回的带有HTML标签的内容
1.本文采用的是android调用assest中的本地.html文件,webview展示(加载网络的html效果是一样的,把url替换掉就行) 2.包含本地.html文件加载,点击.html控件,Android端响应 3.包含本地.html文件加载,点击.html...
Android下使用Webview实现的HTML5视频播放器,播放本地视频时请加载video目录下的1.html
NULL 博文链接:https://android-zhang.iteye.com/blog/1659943
Android WebView 加载本地html,实现JS与原生互调
在android开发webview的时候,有的时候后台不一定给的就是一个url,而是把一些HTML,css,js语言代码给你,然后你自行组装出webview能够识别的语言,并加载到页面当中。 加载html无非有三种情况:一、存放在assets...
1.android webview通过调用js,传递数据,显示在html 上 2.这样的做法会让我们的页面效果更绚丽,显示更随心所欲!
然而,由于HTML5的W3C标准规范还未制定,安卓系统中类浏览器Webview自身存在一些局限性,因此仍存在着诸多问题亟需解决,包括:(1)多窗口类浏览器模式问题。安卓上用于加载的Webview视图窗口只是作为类浏览器而...