OpenURI

本文提供了一个使用Java实现打开单个或多个URI链接的例子。该程序首先检查当前桌面环境是否支持所需的操作,并验证是否支持浏览功能。接着,程序接收命令行参数作为URI链接,并逐一尝试打开。
import java.net.URI;
import java.awt.Desktop;

public class OpenURI {

    public static void main(String [] args) {

        if( !java.awt.Desktop.isDesktopSupported() ) {

            System.err.println( "Desktop is not supported (fatal)" );
            System.exit( 1 );
        }

        if ( args.length == 0 ) {

            System.out.println( "Usage: OpenURI [URI [URI ... ]]" );
            System.exit( 0 );
        }

        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

        if( !desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) {

            System.err.println( "Desktop doesn't support the browse action (fatal)" );
            System.exit( 1 );
        }

        for ( String arg : args ) {

            try {

                java.net.URI uri = new java.net.URI( arg );
                desktop.browse( uri );
            }
            catch ( Exception e ) {

                System.err.println( e.getMessage() );
            }
        }
    }
}
private void handleCodeResult(final QrCodeResult result) { if ((null == mActivity) || mbPause) { CameraLog.d(TAG, () -> "handleCodeResult activity is destroyed"); return; } if (result.isInterrupt()) { return; } QrCodeResult.Type type = result.getType(); String content = result.getContent(); if (type == QrCodeResult.Type.NONE) { searchCode(content); } else if (type == QrCodeResult.Type.WIFI) { Intent intent = new Intent(ACTION_WIFI_SHARE); intent.setPackage(WIRELESS_PACKAGE_NAME); intent.putExtra(EXTRA_WIFI_RAWRESULT, content); try { mActivity.startActivity(intent); reportJumpQrCode(getScreenBrightness(), 1, EventConstant.QRCodeType.TYPE_JUMP_WIFI); } catch (Exception e) { CameraLog.e(TAG, "handleCodeResult, innerHandleCodeResult e: " + e); searchCode(content); } } else { searchCode(content); } } private float getScreenBrightness() { return Settings.System.getInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) (mMaxScreenBrightness * SETTING_BRIGHTNESS_SCALE)); } private boolean searchCode(String content) { if (null == mActivity) { return false; } boolean openUri = false; if (Patterns.WEB_URL.matcher(content).matches() || URLUtil.isValidUrl(content)) { Uri uri = Uri.parse(Util.normalizeUrl(content)); Intent intent = new Intent(Intent.ACTION_VIEW, uri); try { mActivity.startActivity(intent); openUri = true; reportJumpQrCode(getScreenBrightness(), 1, EventConstant.QRCodeType.TYPE_JUMP_HTTP); } catch (Exception e) { CameraLog.e(TAG, "searchCode e: " + e); mbClickJump = false; } } if (!openUri) { Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, content); if (Util.isAppInstalled(getContext(), BROWSER_HEYTAP_PACKAGE_NAME)) { intent.setPackage(BROWSER_HEYTAP_PACKAGE_NAME); } else if (!Util.isAppInstalled(getContext(), BROWSER_CHROME_PACKAGE_NAME)) { intent.setPackage(BROWSER_PACKAGE_NAME); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mActivity.startActivity(intent); openUri = true; reportJumpQrCode(getScreenBrightness(), 1, EventConstant.QRCodeType.TYPE_JUMP_TEXT); } catch (Exception e) { CameraLog.e(TAG, "searchCode e: " + e); mbClickJump = false; } } return openUri; } private Context getContext() { if (null != mActivity) { return mActivity.getApplicationContext(); } return null; } public void addQrCodeJumpView(ViewGroup parentView, ViewGroup previewFrame, RelativeLayout controlPanelLayout) { if ((null == parentView) || (null == mQrCodeJumpView)) { return; } mCameraQrCodeView = previewFrame.findViewById(R.id.qrcode_view); if (null == mCameraQrCodeView) { mCameraQrCodeView = (CameraQrCodeView) mActivity.getLayoutInflater() .inflate(R.layout.view_stub_qrcode_view, previewFrame) .findViewById(R.id.qrcode_view); } if (!mbPause) { mCameraQrCodeView.onResume(); } parentView.removeView(mQrCodeJumpView); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, Optional.ofNullable(getContext()).map( (c) -> c.getResources().getDimensionPixelSize(R.dimen.ai_notice_view_height)) .orElse(Util.dp2px(AI_NOTICE_VIEW_HEIGHT))); layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); layoutParams.addRule(RelativeLayout.ABOVE, controlPanelLayout.getId()); int previewTop = previewFrame.getTop(); int previewHeight = previewFrame.getHeight(); int topMargin = mActivity.getResources().getDimensionPixelSize( ResDecisionManager.getConvertId(R.dimen.pi_ultra_wide_hint_layout_margin_top)); layoutParams.bottomMargin = mActivity.getResources().getDimensionPixelSize(R.dimen.qr_code_vertical_margin_bottom); parentView.addView(mQrCodeJumpView, layoutParams); mQrCodeJumpView.init(topMargin, previewTop, previewHeight, 0); mQrCodeJumpView.setVisibility(View.GONE); View next = mQrCodeJumpView.findViewById(R.id.camera_qr_code_hint_next); boolean rtl = mQrCodeJumpView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; next.setRotation(rtl ? CommonConstant.Orientation.ORIENTATION_180 : CommonConstant.Orientation.ORIENTATION_0); } public void showQrCodeJumpView(boolean animation) { if ((null == mQrCodeJumpView) || (null == mActivity) || (null == mQrCodeScanResult) || mbPause) { return; } if (null != mQrCodeJumpView.getParent()) { mQrCodeJumpView.setOrientation(mOrientation); if (mQrCodeJumpView.isVisibile()) { mQrCodeJumpView.startHideDelay(); } else { mQrCodeJumpView.showQrCodeJumpView(true, animation); ReportThread.postEvent(ReportThread.MSG_EVENT_FUNCTION_SCAN_QR_CODE, () -> DcsReporter.get206Instance().reportScanQRCode(getScreenBrightness(), 1)); } } }你看一下这些代码里有没有问题原因
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值