首先导入service包
implementation 'com.yanzhenjie:andserver:1.1.3'
在assets放入本地网页
创建ServiceUtil类,开启端口为8080的Service,路径为tour文件夹
private void init(){
AssetManager assetManager = mActivity.getAssets();
WebSite webSite = new AssetsWebsite(assetManager, "tour");
server = AndServer.serverBuilder()
.website(webSite)
.port(8080) //服务器要监听的端口
.timeout(10, TimeUnit.SECONDS) //Socket超时时间
.filter(new HttpCacheFilter()) //开启缓存支持
.listener(new Server.ServerListener() {
@Override
public void onStarted() {
Log.e("ServiceUtil","onStarted");
}
@Override
public void onStopped() {
Log.e("ServiceUtil","onStopped");
}
@Override
public void onError(Exception e) {
Log.e("ServiceUtil","onError:"+e.toString());
}
}).build();
start();
}
//启动服务器
public void start(){
if(server!=null) server.startup();
}
//停止服务器
public void stop(){
if(server!=null) server.shutdown();
}
//判断服务器是否在运行
public boolean isRunning(){
if(server!=null) return server.isRunning();
else return false;
}
用webview加载
addWebview("http://localhost:8080/index.html")