Android 中的功能点
Android 隐藏导航栏
<item name="android:windowFullscreen">true</item>
public void setStatusBarFullTransparent() { if (Build.VERSION.SDK_INT >= 21) {//21表示5.0 Window window = getWindow(); // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); // window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); // window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // window.setStatusBarColor(Color.TRANSPARENT); // 隐藏状态栏 getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY ); // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN ); } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //虚拟键盘也透明 //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }
Android的webView唤起打印机
public void printWeb() { Log.i(TAG, "printWeb"); if (mWebView != null) { String name = mWebView.getTitle()+".pdf"; PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); PrintAttributes.Builder builder = new PrintAttributes.Builder(); builder.setColorMode(PrintAttributes.COLOR_MODE_COLOR); printManager.print("Print", mWebView.createPrintDocumentAdapter(name), builder.build()); } }
Android & H5交互
1、Android 设置
mLayoutRoot.addView(mWebView); WebSettings mWebSettings = mWebView.getSettings(); mWebSettings.setSupportZoom(true); mWebSettings.setLoadWithOverviewMode(true); mWebSettings.setUseWideViewPort(true); mWebSettings.setDefaultTextEncodingName("utf-8"); mWebSettings.setAllowContentAccess(true); mWebSettings.setAllowFileAccess(true); mWebSettings.setAllowFileAccessFromFileURLs(false); mWebSettings.setAllowUniversalAccessFromFileURLs(false); mWebSettings.setLoadsImagesAutomatically(true); mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); //新添加,解决个别机型不兼容的问题 mWebSettings.setTextZoom(100); mWebView.getSettings().setUserAgentString(mWebView.getSettings().getUserAgentString()+"lianxin-commom-app"); //自动播放音乐 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { mWebSettings.setMediaPlaybackRequiresUserGesture(false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } //调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface mWebSettings.setJavaScriptEnabled(true); saveData(mWebSettings); newWin(mWebSettings); mWebView.setWebChromeClient(webChromeClient); mWebView.setWebViewClient(webViewClient); addJsListener(this, "NativeBridge"); mWebView.loadUrl(url);
@SuppressLint("JavascriptInterface") public void addJsListener(Object obj, String bridgeName) { mWebView.addJavascriptInterface(obj, bridgeName); }
2、js调用Android的方法
//JS调用Android方法 @JavascriptInterface public String jsCallAndroid(){ Toast.makeText(this,"JS调用Android方法成功",Toast.LENGTH_LONG).show(); return result; }
//JS调用Android方法-带参数 @JavascriptInterface public String jsCallAndroid(int type){ Toast.makeText(this,"JS调用Android方法成功,type----"+type,Toast.LENGTH_LONG).show(); return result; }
如果js 调用的Android的方法内要处理webView 相关的操作,需要在主线程
@JavascriptInterface public void printWeb() { Log.i(TAG, "printWeb"); if (mWebView != null) { mWebView.post(new Runnable() { @Override public void run() { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = dateFormat.format(calendar.getTime()); String name = "咨询报告" + formattedDate + ".pdf"; PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); PrintAttributes.Builder builder = new PrintAttributes.Builder(); builder.setColorMode(PrintAttributes.COLOR_MODE_COLOR); printManager.print("Print", mWebView.createPrintDocumentAdapter(name), builder.build()); } }); } }
3、JS调用Android
// JS中调用Android方法 window.NativeBridge.postMessage(message); window.NativeBridge.printWeb();
4、Android调用js
protected void pushDeviceDataToWeb(String data) { if (mWebView != null) { runOnUiThread(new Runnable() { @Override public void run() { mWebView.loadUrl("javascript:pushDeviceDataToWeb( '" + data + "')"); mWebView.loadUrl("javascript:pushDeviceDataToWebHome( '" + data + "')"); } }); } }
Android WebView a 标签 设置下载监听
//设置下载监听 mWebView.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { //进行下载处理,跳转浏览器或者调用系统下载方法 //保存图片到相册 if (!PermissionTool.requestPermission(WebActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE)) { new Thread(() -> saveImage(url)).start(); } } });
Android 横屏
android:screenOrientation="landscape"
Android 横屏toast 字体变大的处理办法
/** * 更改字体大小 */ protected void initFontScale() { Configuration configuration = getResources().getConfiguration(); configuration.fontScale = 1; //0.85 小, 1 标准大小, 1.15 大,1.3 超大 ,1.45 特大 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); metrics.scaledDensity = configuration.fontScale * metrics.density; getResources().updateConfiguration(configuration, metrics); }
Android 屏幕常亮
if(开启常亮){ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Toast.makeText(this, "屏幕已开启常亮", Toast.LENGTH_SHORT).show(); }else{ getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Toast.makeText(this, "屏幕已关闭常亮", Toast.LENGTH_SHORT).show(); }
Android 去设置页面
private void goToSettings() { Intent intent = new Intent(); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { // Android 5.0及以上 intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); intent.setData(Uri.fromParts("package", getPackageName(), null)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Android 5.0 intent.setAction(Settings.ACTION_SETTINGS); } else { // Android 4.4及以下 intent.setAction(Settings.ACTION_APPLICATION_SETTINGS); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); intent.setData(Uri.parse("package:" + getPackageName())); } startActivity(intent); }
Android 清理数据
// 清理数据 mWebView.clearCache(true); CookieManager.getInstance().removeAllCookies(null); CookieManager.getInstance().flush(); mWebView.clearHistory();