鸿蒙开发经验教训

 

  • WebView的resource response

 

//                    Uri requestUri = request.getRequestUrl();
// String urltrytoload = requestUri.toString();
// if (CheckNetworkStatus.isNetworkConnectedInternet(getContext()) == false) {
//// HiLog.info(mLogLabel, webView.getComponentDescription().toString()+"webViewInFocus ResourceResponse processResourceRequest NOT %{public}s,编号为%{public}d", requestUri.toString(), 123456);
// String response = "";
//// "<html>\n" +
//// "<center><div style=\"width:270px;height:60px;border:0px solid grey;\">"+
//// "<p><font size=\"3\" face=\"arial\" color=\"red\"><center>网址不可达,请检查网络状态!</center></font></p>"+
//// "</div></center>"+
//// "<html>";
// ResourceResponse webResourceResponse = new ResourceResponse("text/html", new ByteArrayInputStream(response.getBytes()), "utf-8");
// HiLog.error(HLQQ, webView.getComponentDescription().toString()+"webViewInFocus webResourceResponse %{public}s,编号为%{public}d", webResourceResponse.getCharset() + webResourceResponse.getReason(), 123480);
//
//// threadtoupdateUi(1);
//
// return webResourceResponse;
//
//
// } else {
// HiLog.error(HLQQ, webView.getComponentDescription().toString()+"webViewInFocus ResourceResponse processResourceRequest YES %{public}s,编号为%{public}d", requestUri.toString(), 123457);
// return super.processResourceRequest(webView, request);
// }



  • 拦截并处理本地资源

                //此段代码有用,用于处理本地文件。不要删除!

// @Override
// public ResourceResponse processResourceRequest(WebView webview, ResourceRequest request) {
//
//
// final String authority = "lingxihelp.com";
// final String rawFile = "/rawfile/";
// final String local = "/local/";
// Uri requestUri = request.getRequestUrl();
// if (authority.equals(requestUri.getDecodedAuthority())) {
// String path = requestUri.getDecodedPath();
// if (TextTool.isNullOrEmpty(path)) {
// return super.processResourceRequest(webview, request);
// }
// if (path.startsWith(rawFile)) {
// // 根据自定义规则访问资源文件
// String rawFilePath = "entry/resources/rawfile/" + path.replace(rawFile, "");
// String mimeType = URLConnection.guessContentTypeFromName(rawFilePath);
// try {
// Resource resource = getResourceManager().getRawFileEntry(rawFilePath).openRawFile();
// ResourceResponse response = new ResourceResponse(mimeType, resource, null);
// return response;
// } catch (IOException e) {
// HiLog.info(mLogLabel, "open raw file failed");
// }
// }
// if (path.startsWith(local)) {
// // 根据自定义规则访问本地文件
// String localFile = getContext().getFilesDir() + path.replace(local, "/");
// HiLog.info(mLogLabel, "open local file " + localFile);
// File file = new File(localFile);
// if (!file.exists()) {
// HiLog.info(mLogLabel, "file not exists");
// return super.processResourceRequest(webview, request);
// }
// String mimeType = URLConnection.guessContentTypeFromName(localFile);
// try {
// InputStream inputStream = new FileInputStream(file);
// ResourceResponse response = new ResourceResponse(mimeType, inputStream, null);
// return response;
// } catch (IOException e) {
// HiLog.info(mLogLabel, "open local file failed");
// }
// }
// }
// return super.processResourceRequest(webview, request);
// }

 

  • 以schedule方式run

//                        HiLog.warn(HLQQ, "++++++++++updateprogress");
// progress2 = newValue;
// timerTask2 = new TimerTask() {
// @Override
// public void run() {
// myEventHandlerBoundwithmainrunner.sendEvent(12);
// }
// };
// timer2 = new Timer();
// timer2.schedule(timerTask2, 1000, 1000);

 

  • ClipBoard

        SystemPasteboard pasteboard = SystemPasteboard.getSystemPasteboard(this);
PasteData pasteData = new PasteData();
pasteData.addTextRecord("转自外媒-:" + wv.getTitle() + wv.getFirstRequestUrl());
// PacMap pacMap = new PacMap();
// pacMap.putString("ADDITION_KEY", "ADDITION_VALUE_OF_TEXT");
// pasteData.getProperty().setAdditions(pacMap);
// pasteData.getProperty().setTag("USER_TAG");
// pasteData.getProperty().setLocalOnly(true);
pasteboard.setPasteData(pasteData);
// pasteData = null;


  • 启动各种各样的Intent


// Intent intent = new Intent();
// Operation operation = new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();
// intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
// intent.setType("text/plain");
// intent.setOperation(operation);
// startAbility(intent);

// Intent intent = new Intent();
// Set<String> entities = new HashSet<>();
// entities.add("android.intent.category.LAUNCHER");
// Operation operation = new Intent.OperationBuilder()
// .withDeviceId("")
// .withBundleName("com.tencent.mm")
// .withAbilityName("com.tencent.mm.ui.LauncherUI")
// .withAction("android.intent.action.MAIN")
// .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
// .withEntities(entities)
// .build();
// intent.setOperation(operation);
// startAbility(intent);


// IBundleManager manager = this.getBundleManager();
// try {
// System.out.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQ"+manager.getAllFormsInfo().toArray().toString());
// } catch (RemoteException e) {
// e.printStackTrace();
// }

// Intent intent = new Intent();
// intent.setType("text/plain");
// Set<String> entities = new HashSet<>();
// entities.add("android.intent.category.LAUNCHER");
// Operation operation = new Intent.OperationBuilder()
// .withDeviceId("")
//// IT's ok'
//// .withBundleName("com.autonavi.minimap")
//// .withAbilityName("com.autonavi.map.activity.SplashActivity")
//// .withBundleName("com.sina.weibo")
// //这是启动页
//// .withAbilityName("com.sina.weibo.SplashActivity")
// //这是功能页
//// .withAbilityName("com.sina.weibo.MainTabActivity")
// //这是写微博的地方。
//// .withAbilityName("com.sina.weibo.composerinde.OriginalComposerActivity")
// .withAction("android.intent.action.MAIN")
// .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
// .withEntities(entities)
// .build();
// intent.setOperation(operation);
// startAbility(intent);

// outsideIntent = new Intent();
//// intent.setType("text/plain");
// Set<String> entities = new HashSet<>();
// entities.add("android.intent.category.LAUNCHER");
//// entities.add("android.intent.category.DEFAULT");
//// entities.add("android.intent.category.HOME");
// Operation operation = new Intent.OperationBuilder()
// .withDeviceId("")
// .withBundleName("com.huawei.android.internal.app")
// .withAbilityName("com.huawei.android.internal.app.instantshare.HwInstantShareActivity")
// .withAction("android.intent.action.MAIN")
// .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
// .withEntities(entities)
// .build();
// outsideIntent.setOperation(operation);
// startAbility(outsideIntent);

// private boolean isAppExist(Context context, String appPkg) {
// manager = context.getBundleManager();
// if (verifySelfPermission("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED") != IBundleManager.PERMISSION_GRANTED) {
// // 应用未被授予权限
// if (canRequestPermission("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED")) {
// // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
// requestPermissionsFromUser(
// new String[]{"ohos.permission.GET_BUNDLE_INFO_PRIVILEGED"}, 9090);
// } else {
// // 显示应用需要权限的理由,提示用户进入设置授权
// new ToastDialog(getContext()).setText("请进入系统设置进行授权").show();
// }
// } else {
//
// }
// return true;
//

  • PopDiglog版toast

private void manyToast(String textshown) {

DirectionalLayout webToastLayoutforsoftkeyforward = (DirectionalLayout) LayoutScatter.getInstance(getContext())
.parse(ResourceTable.Layout_web_toast_xward, null, false);
((Text) webToastLayoutforsoftkeyforward.getComponentAt(0)).setText(textshown);
((Text) webToastLayoutforsoftkeyforward.getComponentAt(0)).setFont(Font.DEFAULT_BOLD);
webToastLayoutforsoftkeyforward.setBackground(elementToast);
PopupDialog toastMadeMyself = new PopupDialog(getContext(), networkNotAvailablePage);
toastMadeMyself.setCustomComponent(webToastLayoutforsoftkeyforward);
toastMadeMyself.setDuration(1000);
toastMadeMyself.setCornerRadius(160);
toastMadeMyself.showOnCertainPosition(LayoutAlignment.TOP, 0, 320);
toastMadeMyself.show();
}
  • 各种更新UI的方法

调用主UI线程异步和延时异步、定时间隔、通过进程间通讯事件

 

 

 

 

 

 

 

上一篇:ASP.NET Core学习之六 MYSQL批量插入


下一篇:自然语言处理NLP学习-基本目标和应用领域