2010-09-18 50 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了解如何使用反射在给定类中调用给定方法的示例。