2015-11-04 38 views
0

我有我的android應用程序的web視圖。我只想在瀏覽器上查看網頁時才顯示橫幅。我創建了一個界面,它測試的很好,但由於網頁無法識別該功能,因此該頁面上的其他javascript無法加載。這是我的代碼:JavascriptInterface函數崩潰在網頁上的JavaScript

接口類

import android.content.Context; 
import android.webkit.JavascriptInterface; 


public class WebAppInterface { 
Context mContext; 

/** Instantiate the interface and set the context */ 
WebAppInterface(Context c) { 
    mContext = c; 
} 

/** Show a toast from the web page */ 
@JavascriptInterface 
public Boolean getUser() { 

    return true; 
} 
} 

活動課

public class MainActivity extends Activity { 


    ProgressBar progressBar; 
    WebView webview; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 





     setContentView(R.layout.activity_main); 

     webview = (WebView)findViewById(R.id.webview); 
     progressBar = (ProgressBar)findViewById(R.id.progressBar); 
     webview.addJavascriptInterface(new WebAppInterface(this),"Android"); 
     this.webview.getSettings().setUserAgentString(
       this.webview.getSettings().getUserAgentString() 
         + " " 
         + getString(R.string.user_agent_suffix) 
     ); 
     WebSettings settings = webview.getSettings(); 
     settings.setJavaScriptEnabled(true); 


     webview.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
       progressBar.setProgress(progress); 
       if (progress == 100) { 
        progressBar.setVisibility(View.GONE); 

       } else { 
        progressBar.setVisibility(View.VISIBLE); 

       } 
      } 
     }); 





     webview.setWebViewClient(new WebViewClient() { 

      public boolean shouldOverrideUrlLoading(WebView view, String url) { 

       view.loadUrl(url); 
       return true; 
      } 


     }); 
     webview.loadUrl("http://www.getonfleek.com"); 
     ParsePush.subscribeInBackground("test"); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
    @Override 
    public void onBackPressed() { 
     if (webview.canGoBack()) { 
      webview.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } 




} 

HTML

</script> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 
     var user = Android.getUser(); 
     if(user == null){ 
     //do something on web 
}else{ 
//do something in app 
} 
}); 
</script> 

回答

1

檢查以確保界面試圖調用它之前存在。

<script type="text/javascript"> 
    $(document).ready(function(){ 
     var user = null; 
     if(typeof Android !== 'undefined'){ 
      user = Android.getUser(); 
      //do something in app 
     }else{ 
      //do something on web 
     } 
    }); 
</script> 
+0

賈斯汀 - 我只是試過,但因爲它不承認'安卓'它不執行其餘的代碼。說'Android'是未定義的。如果函數未定義,有沒有辦法繼續? –

+0

我的歉意,我錯誤地寫了我的答案。我已經更新了它。 –

+0

謝謝賈斯汀。我正在嘗試,但我的語法已關閉。很棒! –