2

我在Android開發網站上閱讀了關於使用反射的內容。但我真的不知道如何使用它。我需要這個Java文件在1.5(SDK3)設備上運行,但只是忽略新的代碼,它在2.0(SDK5)或更高版本的手機上運行良好。我有這個活動(在下面發佈),它是一個簡單的web視圖。但是,我希望啓用地理定位(即使它僅適用於2.0及更高版本的手機),因爲這些API在SDK 5之前未引入,並且我希望webview至少能夠加載在1.5手機而不​​是隻是崩潰。我如何獲取我的代碼並使用反射進行設置?在Android中使用反射以實現向後兼容性

package com.my.app; 

    import com.facebook.android.R; 
    //NEEDS TO BE IGNORED********************************************************** 
    import android.webkit.GeolocationPermissions; 
    import android.webkit.GeolocationPermissions.Callback; 
    //END************************************************************************** 
    import android.app.Activity; 
    import android.app.ProgressDialog; 
    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.KeyEvent; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.webkit.CookieSyncManager; 
    import android.webkit.WebChromeClient; 
    import android.webkit.WebView; 
    import android.webkit.WebViewClient; 
    import android.widget.Toast; 

    //GeolocationPermissionsCallback NEEDS TO BE IGNORED********************************************************** 
    public class Places extends Activity implements GeolocationPermissions.Callback { 
     private ProgressDialog progressBar; 
     public WebView webview; 
     private static final String TAG = "Main"; 
     String geoWebsiteURL = "http://google.com"; 

     @Override 
     public void onStart() 
     { 
      super.onStart(); 
      CookieSyncManager.getInstance().sync(); 
     } 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      CookieSyncManager.createInstance(this); 
      CookieSyncManager.getInstance().startSync(); 

      webview = (WebView) findViewById(R.id.webview); 
      webview.setWebViewClient(new testClient()); 
      webview.getSettings().setJavaScriptEnabled(true); 
      webview.getSettings().setPluginsEnabled(true); 
      webview.loadUrl("http://google.com"); 

      progressBar = ProgressDialog.show(Places.this, "", "Loading Page..."); 

      //START GROUP OF CODE THAT NEEDS TO BE IGNORED************************************************************ 
      webview.getSettings().setGeolocationEnabled(true); 

      GeoClient geo = new GeoClient(); 
      webview.setWebChromeClient(geo); 
     } 

     public void invoke(String origin, boolean allow, boolean remember) { 

     } 

     final class GeoClient extends WebChromeClient { 

      @Override 
      public void onGeolocationPermissionsShowPrompt(String origin, 
      Callback callback) { 
       super.onGeolocationPermissionsShowPrompt(origin, callback); 
       callback.invoke(origin, true, false); 
      } 

    //END OF CODE THAT NEEDS TO BE IGNORED************************************************ 
    } 

    private class testClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      Log.i(TAG, "Processing webview url click..."); 
      view.loadUrl(url); 
      return true; 
     } 

     public void onPageFinished(WebView view, String url) { 
      Log.i(TAG, "Finished loading URL: " +url); 
      if (progressBar.isShowing()) { 
       progressBar.dismiss(); 
      } 

      if (url.startsWith("mailto:") || url.startsWith("geo:") || 
       url.startsWith("tel:")) { 

       Intent intent = new Intent 
        (Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
      } 
     } 
    } 

     //What's here???? 

     public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
       webview.goBack(); 
       return true; 
      } 
      if (keyCode == KeyEvent.KEYCODE_SEARCH) { 
       Intent z = new Intent(this, Search.class); 
       startActivity(z); 
      } 
      return super.onKeyDown(keyCode, event); 
     } 

     public boolean onCreateOptionsMenu (Menu menu) { 
      super.onCreateOptionsMenu(menu); 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.menu, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected (MenuItem item) { 
      switch (item.getItemId()) { 
       case R.id.home: 
        Intent m = new Intent(this, Home.class); 
        startActivity(m); 
        return true; 

       case R.id.refresh: 
        webview.reload(); 
        Toast.makeText(this, "Refreshing...", Toast.LENGTH_SHORT).show(); 
        return true; 
      } 
      return false; 
     } 

     public void onStop() 
     { 
      super.onStop(); 
      CookieSyncManager.getInstance().sync(); 
     } 
    } 

回答

1

步驟1:不要在活動上實施GeolocationPermissions.Callback。使用其他對象GeolocationPermissions.Callback,在公共Java類中實現一個對象,如果您在舊版本的操作系統上運行,則不會加載該對象。

步驟#2:閱讀關於the question you asked previously的答案和評論,其中涵蓋了你需要知道的其餘內容。值得注意的是,您將使GeoClient成爲其自己文件中的公共Java類,因此您可以避免在舊版本的操作系統上加載它。就我個人而言,我喜歡Stephen C的答案,並且我證明了它的使用here