1,android源生的跳转:
firstUrl = “http://baidu.com“;
Uri uri = Uri.parse(firstUrl);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
2,通过webView跳转
private void init() {
myWebView = (WebView) findViewById(R.id.webView);
Intent intent = getIntent();
String webUrl =intent.getStringExtra(“url”);
myWebView.getSettings().setJavaScriptEnabled(true);
// myWebView.setScrollBarStyle();
WebSettings webSettings = myWebView.getSettings();
webSettings.setAllowFileAccess(true);
webSettings.setBuiltInZoomControls(true);
myWebView.loadUrl(webUrl);
//加载数据
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
MyWebViewActivity.this.setTitle("加载完成");
} else {
MyWebViewActivity.this.setTitle("加载中.......");
}
}
});
这是两种跳转,现有有这么一个需求:
如果手机上有浏览器的话,用webView跳转会有跳2次的情况,具体原因还没找到。如果不用webview的话,没有浏览器的情况会出现崩溃。
1,首先判断系统上是否有浏览器
public class HasBrowser {
public static boolean hasBrowser(Context context){
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(“http://”));
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);
final int size = (list == null) ? 0 : list.size();
return size > 0;
}
}
2,运用工具类做判断
@Override
public FocusReturnType onKeyCenter() {
if (currentFocus == 0) {
firstUrl = “https://baidu.com“;
} else {
firstUrl = “https://sina.com“;
}
// showQrDialog();
// Uri uri = Uri.parse(firstUrl);
// Intent it = new Intent(Intent.ACTION_VIEW, uri);
// startActivity(it);
if (HasBrowser.hasBrowser(getActivity())) {
Uri uri = Uri.parse(firstUrl);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
return FocusReturnType.hasFocus;
} else {
Intent intent = new Intent();
intent.putExtra(“url”, firstUrl);
intent.setClass(getActivity(), MyWebViewActivity.class);
startActivity(intent);
return FocusReturnType.hasFocus;
}
}
需要加入权限:
界面两个图片的焦点选择:
@Override
public void onFocus(IUIViewBase iuiViewBase, boolean b) {
if (b) {
layout.setBackgroundResource(R.drawable.home_sc_selected);
} else {
layout.setBackgroundResource(R.drawable.home_sc_selected_tm);
}
}