2010-09-18 46 views
2

我一直在閱讀有關反射的android文檔,但我只是沒有真正掌握如何實際使用它。是否需要創建多個類文件?是否將它分解到同一個類文件中的某些部分?我想理解它,我真的這樣做,但我不明白。爲了向後兼容需要使用反射的幫助

無論如何,我有這個簡單的webview代碼,想要使用來自SDK7的新地理位置代碼,但它使1.5和1.6手機崩潰。我真的希望能夠使這個java類工作(至少在加載html時),即使在sdk7之前地理定位不起作用。我已經把所有的代碼放到了eclipse下面的紅線下,當SDK在7以下的時候,會有人介意用我的代碼向我展示如何使用反射?我真的很感激它。我讀過沼澤和文件,在閱讀它們之後嘗試的所有內容都不起作用。

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); 
         } 
      } 
     } 



      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

將您的7代碼重構爲一個單獨的類,並且不會在現有代碼中引用此新類。

然後您需要一種方法來確定您需要的設施是否存在。在我看來,你可以使用「is android.webkit.GeolocationPermissions present」這是最簡單的,因爲你可以使用Class.forName("android.webkit.GeolocationPermissions"),然後使用反射來首先加載你上面寫的單獨的類,然後調用你想運行的方法你的班。這將確保您的代碼不會向JVM指出您的單獨類甚至存在,直到您在運行時確定需要它爲止。

請參閱http://www.exampledepot.com/egs/java.lang.reflect/Methods.html瞭解如何使用反射在給定類中調用給定方法的示例。