Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中使用反射实现向后兼容性_Java_Android_Reflection_Backwards Compatibility_Android 1.5 Cupcake - Fatal编程技术网

Java 在Android中使用反射实现向后兼容性

Java 在Android中使用反射实现向后兼容性,java,android,reflection,backwards-compatibility,android-1.5-cupcake,Java,Android,Reflection,Backwards Compatibility,Android 1.5 Cupcake,我在Android开发网站上读到了关于使用反射的内容。但我真的不知道如何使用它。我需要这个Java文件在1.5(SDK3)设备上运行,但只需忽略新代码,它就可以在2.0(SDK5)或更高版本的手机上正常工作。我有这个活动(贴在下面),它是一个简单的网络视图。但是,我希望启用地理定位功能(即使它仅适用于2.0及更高版本的手机),因为这些API直到SDK 5才引入,我希望webview至少能够在1.5手机上加载,而不仅仅是崩溃。如何获取代码并使用反射进行设置 package com.my.a

我在Android开发网站上读到了关于使用反射的内容。但我真的不知道如何使用它。我需要这个Java文件在1.5(SDK3)设备上运行,但只需忽略新代码,它就可以在2.0(SDK5)或更高版本的手机上正常工作。我有这个活动(贴在下面),它是一个简单的网络视图。但是,我希望启用地理定位功能(即使它仅适用于2.0及更高版本的手机),因为这些API直到SDK 5才引入,我希望webview至少能够在1.5手机上加载,而不仅仅是崩溃。如何获取代码并使用反射进行设置

    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:不要对活动实施
GeolocationPermissions.Callback
。对
GeolocationPermissions.Callback
使用其他对象,该对象在公共Java类中实现,如果在较旧版本的操作系统上运行,则不会加载该类

第二步:阅读下面的答案和评论,其中包含了你接下来需要知道的内容。值得注意的是,您可以将
GeoClient
作为自己文件中的公共Java类,这样就可以避免将其加载到较旧版本的操作系统上。就个人而言,我喜欢Stephen C答案,并演示了它的用法