2016-08-24 36 views
0

我最近意識到,我的android應用程序中顯示超鏈接圖像的webview不再在調用瀏覽器(我猜是默認行爲)時調用瀏覽器,而是在內部打開url內容。我只對MainActivity.java進行了一些更改,所以我想知道它是否可能與這些更改有關。我已經發布了我之前/之後的代碼。任何幫助表示讚賞。 前:爲什麼在打開URL時我的WebView行爲發生了變化?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


    String bannerURL="http://www.dadhesab.ir/appbanner"; 
    WebView myWebView = (WebView) findViewById(R.id.webview); 
    final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); 
    if (activeNetwork != null && activeNetwork.isConnected()) 
    { 
     myWebView.loadUrl(bannerURL); 
     myWebView.setVisibility(View.VISIBLE); 




    } 
    else 
    { 

    } 
    TextView copyrightnotice=(TextView) findViewById(R.id.copyrightNotice); 
    int year = Calendar.getInstance().get(Calendar.YEAR); 
    copyrightnotice.setText(String.format(getString(R.string.copyrightnotice)) + " © " + year); 

} 

後:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 



    String bannerURL="http://www.dadhesab.ir/appbanner"; 
    final WebView myWebView = (WebView) findViewById(R.id.webview); 
    final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); 
    if (activeNetwork != null && activeNetwork.isConnected()) 
    { 
     myWebView.loadUrl(bannerURL); 
     myWebView.setWebViewClient(new WebViewClient() { 

      public void onPageFinished(WebView view, String url) { 
       // do your stuff here 
       myWebView.setVisibility(View.VISIBLE); 

      } 
     }); 

     try { 
      // Create a URL for the desired page 
      URL url = new URL("http://dadhesab.ir/appversion.txt"); 

      // launch task 
      new ReadTextTask().execute(url); 
     } 
     catch (MalformedURLException e) { 
      // ** do something here ** 
     } 


    } 
    else 
    { 

    } 
    TextView copyrightnotice=(TextView) findViewById(R.id.copyrightNotice); 
    int year = Calendar.getInstance().get(Calendar.YEAR); 
    copyrightnotice.setText(String.format(getString(R.string.copyrightnotice)) + " © " + year); 

} 

private class ReadTextTask extends AsyncTask<URL, Void, String> { 
    String latestvc = null; 

    @Override 
    protected String doInBackground(URL... urls) { 

     try { 
      // Read all the text returned by the server 
      BufferedReader in = new BufferedReader(new InputStreamReader(urls[0].openStream())); 
      latestvc = in.readLine(); 
      in.close(); 
     } 
     catch (IOException e) { 
      // ** do something here ** 

     } 
     return latestvc; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     int versionCode = BuildConfig.VERSION_CODE; 

     if (latestvc!=null)//to avoid network conflicts or missing file 
     { 
      if(versionCode < (Integer.parseInt(latestvc))) 
      { 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         context); 

       // set title 
       alertDialogBuilder.setTitle("توجه!"); 

       // set dialog message 
       alertDialogBuilder 
         .setMessage("نسخه جدید دادحساب رسید!") 
         .setPositiveButton("بروزرسانی",new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int id) { 
           // if this button is clicked, do sth. 
           Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://cafebazaar.ir/app/ir.dadhesab.dadhesab/?l=fa")); 
           startActivity(browserIntent);       } 
         }) 
         .setNegativeButton("لغو",new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int id) { 
           // if this button is clicked, just close 
           // the dialog box and do nothing 
           dialog.cancel(); 
          } 
         }); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
      } 
     } 


    } 
} 

回答

2

檢查這個帖子:Support for other protocols in Android webview

你需要重寫shouldOverridUrlLoading方法:

webview.setWebViewClient(new WebViewClient(){ 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url != null && url.startsWith("http://")) { 
      view.getContext().startActivity(
       new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
      return true; 
     } else { 
      return false; 
     } 
    } 
}); 

學分:sven

相關問題