如何忽略类中的某些方法和代码?基于SDK

发布于 2024-09-15 21:11:57 字数 5244 浏览 13 评论 0原文

我正在阅读http://android-developers。 blogspot.com/2009/04/backward-compatibility-for-android.html 但我真的不明白如何忽略某些代码行。我有这个活动(发布在下面),它是一个简单的网络视图。但是,我希望启用地理定位(即使它仅适用于 2.0 及更高版本的手机),因为这些方法直到 SDK 5 (android 2.0) 才引入,并且我希望 webview 至少能够能够在 1.5 手机上加载而不只是崩溃。当用户手机 SDK 低于 SDK 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...");

        //THIS 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();

        }




} 

I was reading over http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html But I'm really not grasping how to ignore certain lines of code. I have this Activity (posted below) and it's a simple webview. However, I want to have geolocation enabled(even if it is only for 2.0 and up phones), since these methods weren't introduced until SDK 5 (android 2.0) and I would like the webview to be able to at the very least be able to load on a 1.5 phone rather than just crashing. Could someone possibly show me how to take this code and make it ignore the lines of code that i pointed out with the starred comments when the users phone SDK is LESS than SDK 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...");

        //THIS 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();

        }




} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-09-22 21:11:57

对于您的问题,(至少)有两种可能的解决方案:

  1. 针对不同操作系统的不同版本 - 为不同操作系统创建不同的软件版本。这是一种常见的方法,许多开源项目为 Linux、Windows、Mac 操作系统提供不同的版本。您的情况类似,您可以为 android 1.5、2.0、... 创建不同的版本

  2. 适用于所有 Android 操作系统的一个版本 - 如果您可以找到一种方法来检测操作系统的版本运行时,然后您可以重新设计代码以在不同的操作系统上运行。您可以为 android 1.6 和 2.0 添加特殊类,并确保仅加载正确的类。

代码的简单示例:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // ... 

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

    if (isGeolocationAvailable()) {
       Android20Util.enableGeolocation(webview);
    }        
}

Android20Util 包含一些静态方法,例如:

public static void enableGeolocation(WebView webview) {
    webview.getSettings().setGeolocationEnabled(true);
    webview.setWebChromeClient(new GeoClient());
}

There are (at least) two possible solutions for your problem:

  1. Different versions for different OS - create different builds of your software for different OS. This is a common approach, a lot of open source projects offer different builds for linux, windows, mac os. Your case is similiar, you could create different builds for android 1.5, 2.0, ...

  2. One version for all Android OS - If you can find a way to detect the version of the OS at runtime, then you can redesign your code to run on different OS. You can add special classes for android 1.6 and 2.0 and make sure, that only the correct classes are loaded.

A quick example for your code:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // ... 

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

    if (isGeolocationAvailable()) {
       Android20Util.enableGeolocation(webview);
    }        
}

Android20Util contains some static methods, like:

public static void enableGeolocation(WebView webview) {
    webview.getSettings().setGeolocationEnabled(true);
    webview.setWebChromeClient(new GeoClient());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文