您的位置:首页 > 移动开发 > Android开发

Android之项目开发中在app内Intent意图跳转自己指定的Html5页面

2016-08-17 15:41 1866 查看

Android之项目开发中在app内Intent意图跳转自己指定的Html5页面

常规的跳转我们都知道是用Intent,但是Intent一般用于Activity间的跳转,这里我们只有一个URL,那么怎样实现跳转呢?

下面开始

Class.forName(“Test”)适用多个跳转页面String传值

Intent intent = new Intent();
intent.putExtra("key", "value");
try {                                       intent.setClass(context.getApplicationContext()
,Class.forName("Test"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );                context.getApplicationContext().startActivity(intent);


Test.class单一的

Intent intent = new Intent();
intent.putExtra("key", "value");
try {                                           intent.setClass(context.getApplicationContext(),Test.class);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );                context.getApplicationContext().startActivity(intent);


直接跳到手机浏览器,不在app里面了

Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.baidu.com/");
intent.setData(content_url);
startActivity(intent);


跳转手机浏览器算什么,我直接打开其他应用,例如美团地址打开高德地图导航,相信大家不陌生吧

ComponentName componentName=new ComponentName("com.lany.screenshot","com.lany.screenshot.MainActivity");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("Version", getSimpleVersionInfo());
intent.putExtra("buildNumber", buildNumber);
startActivity(intent);


除了跳转,还可以附带数据,注意在跳转到其他app时,跳转到的app必须在跳转到的界面的manifest文件中定义exported=true,否则无法正常跳转。

这个也是H5但是却跳出了自己的app,工程中不太需要

Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.baidu.com/");
intent.setData(content_url);
startActivity(intent);


给大家来个秒杀其他博客的东西,项目实在开发必须

其实兼容Html5网页的最好的方式是如下。。。

Intent intent = new Intent();
intent.putExtra("H5Url", "https://www.baidu.com/");                   intent.setClass(context.getApplicationContext(),H5WebViewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );                context.getApplicationContext().startActivity(intent);


H5WebViewActivity,这个自己随便找个,主要用处就是webView的使用,不会的百度吧

这个是样式,不能直接copy,这种写法就用到了intent的传值,其他啥也没有但是,你可以传任何字段

public class H5WebViewActivity extends JSONWadeActivity {

private WebView webview;//调用内置浏览器控件

private ProgressDialog myDialog;//进度对话框
/*
* 定时规则:
* 开始加载页面时,启动定时器,页面加载完成时关闭定时器,超过timeout时间还没有加载完成的页面,定时任务停止页面加载并关闭定时器
* 如果网页内部通过iframe进行提交查询,本定时规则无效
* 可能会异常退出,取消定时器
*/
private String mainUrl;//首页
private String currentUrl;//当前url
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_h5);

webview=(WebView)findViewById(R.id.lsWebViewH5);//获取控件
webview.clearHistory();//清空访问历史
webview.setBackgroundColor(Color.TRANSPARENT);//设置webview背景色透明,使背景图片可见
WebSettings setting=webview.getSettings();
setting.setJavaScriptEnabled(true);//可以使用javascript,有风险
setting.setSupportZoom(true);//支持缩放
setting.setBuiltInZoomControls(true);//出现缩放工具
//webview.addJavascriptInterface(new JsInterface(), "JsInterface");//js脚本可以调用的类
//在当前webview中打开连接,不要跳到系统浏览器中
webview.setWebViewClient(new WebViewClient(){

//请求页面失败
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
//请求失败的处理404
view.stopLoading();
view.clearView();
Message msg=handler.obtainMessage();
msg.what=1;
handler.sendMessage(msg);

}

//开始请求页面
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//Log.d("GERL", "onPageStarted url="+url);
showMyDialog();//显示进度窗口
currentUrl=url;//记录当前页面地址
//startTimer();//开启计时器
}

//请求页面加载完成
@Override
public void onPageFinished(WebView view, String url) {
//Log.d("GERL", "onPageFinished");
closeMyDialog();//关闭进度提示
super.onPageFinished(view, url);
//stopTimer();//不管是否加载完了,到时将定时器停止
}
});
//设定一个WebChromeClient否则js alert不能弹出
webview.setWebChromeClient(new WebChromeClient() {

// 重写js alert方法,调用android对话框显示,否则会显示网页地址,如“10.0.2.2处的页面表明”
@Override
public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
AlertDialog ab = new AlertDialog.Builder(
H5WebViewActivity.this).setTitle("提示")
.setMessage(message).setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which) {
// 点击确定关闭
dialog.dismiss();
}
}).create();
ab.show();
result.cancel();//不写这句,弹窗后不能点击页面,写了这句,不能阻止js继续向下执行
return true;
// return super.onJsAlert(view, url, message, result);
}
@Override
public boolean onJsConfirm(WebView view, String url,
String message, JsResult result) {
// TODO Auto-generated method stub
return super.onJsConfirm(view, url, message, result);
}
});
//开始请求页面
mainUrl = getIntent().getStringExtra("H5Url");
webview.loadUrl(mainUrl);
}
/**
* 重写按键响应,在WebView中查看网页时,按返回键的时候按浏览历史退回,如果不做此项处理则整个WebView返回退出
*/
public boolean onKeyDown(int keyCode,KeyEvent event){
//Log.d("GERL", "onKeyDown=="+keyCode);
if(keyCode==KeyEvent.KEYCODE_BACK){
//Log.d("GERL", "onKeyDown goBack");
//返回键,当前页面不是首页的返回首页,是首页的就退出
if(currentUrl!=null && !currentUrl.equals(mainUrl)){
webview.loadUrl(mainUrl);
return true;
}
/*
if(webview.canGoBack()){
webview.goBack();
return true;
}*/
}
return super.onKeyDown(keyCode, event);
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
closeMyDialog();//注销的时候把dialog也注销吧
super.onDestroy();
}

@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){//消息处理器
@Override
public void handleMessage(Message msg) {
//Log.d("GERL", "handleMessage what=="+msg.what);
closeMyDialog();
switch (msg.what){
case 1://加载失败
Toast.makeText(H5WebViewActivity.this, "请求数据失败!",Toast.LENGTH_SHORT).show();
//webview.loadUrl("file:///android_asset/ls404.html");
finish();
break;
case 2://加载超时
Toast.makeText(H5WebViewActivity.this, "请求超时失败!",Toast.LENGTH_SHORT).show();
webview.stopLoading();
webview.loadUrl("file:///android_asset/ls404.html");
break;
}
}
};
/**
* 显示进度窗口
*/
public void showMyDialog() {
myDialog = new ProgressDialog(this);
myDialog.setMessage("正在请求数据,请稍等...");
myDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
myDialog.show();
}
/**
* 关闭进度窗口
*/
public void closeMyDialog(){
if(myDialog!=null && myDialog.isShowing()){
myDialog.dismiss();
}
}
/**
* k值
* @param managerId
* @return
*/
private int getK(String managerId){
int a=0;
if(managerId==null || managerId.length()==0){
a=88;
}else{
for(int i=0;i<managerId.length();i++){
a+=managerId.charAt(i);
}
}
int b=a/2;
Calendar t=Calendar.getInstance();
int c=t.get(Calendar.DATE);
int d=t.get(Calendar.MONTH);
return ((a+c+d+922)*b%2014);
}
}


好了完成了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android string html5 url app
相关文章推荐