Exception
當要委託手機/平板中已安裝的瀏覽器 APP 拜 Google 大神時,可能會遇到手機/平板內並沒有安裝 browser,所以要有例外防錯。
- String sGoogle = "https://www.google.com.tw/search?q="; // 要查詢的關鍵字字串
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
- { // os 版本為 android 7.0(Nougat, API24)(含) 以上
- // 將自己的 webview 內的網頁內容濾除 html tag, 只留下純文字
- sGoogle = sGoogle + Html.fromHtml(nowQuestion, Html.FROM_HTML_MODE_LEGACY).toString();
- }
- else
- { // 將自己的 webview 內的網頁內容濾除 html tag, 只留下純文字
- sGoogle = sGoogle + Html.fromHtml(nowQuestion).toString();
- }
- Uri webpage = Uri.parse(sGoogle);
- Intent myIntent = new Intent(Intent.ACTION_VIEW, webpage);
- try
- {
- startActivity(myIntent);
- }
- catch(ActivityNotFoundException e1)
- {
- // 當手機/平板沒有裝瀏覽器(browser) APP 時,跳出提示
- Toast.makeText(getApplicationContext(), "請安裝瀏覽器 APP",Toast.LENGTH_LONG).show();
- }
- catch(Exception ee)
- { // 其他未知的例外錯誤
- Toast.makeText(getApplicationContext(), "未知錯誤:"+ee.getMessage(),Toast.LENGTH_LONG).show();
- }
當 手機/平板 沒有安裝瀏覽器 APP 時,跳出提示
當 手機/平板 安裝數個瀏覽器時,跳出讓使用者選擇的對話視窗