如何忽略某些方法和code一类?基于SDK [英] How to Ignore certain methods and code in a Class? Based on SDK

查看:217
本文介绍了如何忽略某些方法和code一类?基于SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读了的http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html但我真的不抓怎么不理的code的某些行。我有这样的活动(以下公布),这是一个简单的web视图。不过,我想有地理位置启用(哪怕是只针对2.0及以上手机),因为这些方法都没有出台,直到SDK 5(安卓2.0),我想在web视图,以便能够最起码是能够在1.5手机加载,而不仅仅是崩溃。可能有人可能告诉我如何借此code,使之忽略code,我指出了星号标记注释行,当用户手机SDK比SDK 5少?

 包com.my.app;

进口com.facebook.android.R;
// NEEDS被忽略******************************************** **************
进口android.webkit.GeolocationPermissions;
进口android.webkit.GeolocationPermissions.Callback;
//结束*********************************************** ***************************
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.webkit.CookieSyncManager;
进口android.webkit.WebChromeClient;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
进口android.widget.Toast;

// GeolocationPermissionsCallback NEEDS被忽略******************************************* ***************
公共类的地方扩展活动实现GeolocationPermissions.Callback {
        私人ProgressDialog进度;
        公众的WebView的WebView;

        私有静态最后字符串变量=主;


        字符串geoWebsiteURL =htt​​p://google.com;

        @覆盖
         公共无效的OnStart()
        {
           super.onStart();


           CookieSyncManager.getInstance()同步()。

        }




    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        CookieSyncManager.createInstance(本);
        。CookieSyncManager.getInstance()startSync();

        的WebView =(web视图)findViewById(R.id.webview);
        webview.setWebViewClient(新TestClient的());
        webview.getSettings()setJavaScriptEnabled(真)。
        webview.getSettings()setPluginsEnabled(真)。
        webview.loadUrl(http://google.com);

        进度= ProgressDialog.show(Places.this,,加载页面...);

        //这需要被忽略******************************************* *****************
        webview.getSettings()setGeolocationEnabled(真)。



        GeoClient地缘=新GeoClient();
        webview.setWebChromeClient(GEO);

    }

    公共无效的invoke(字符串出身,布尔允许,布尔记住){

    }

    最后一类GeoClient扩展WebChromeClient {


    @覆盖
    公共无效onGeolocationPermissionsShowPrompt(字符串产地,
    回调回调){
    super.onGeolocationPermissionsShowPrompt(产地,回调);
    callback.invoke(原产地,真,假);
    }
//结束code需要被忽略************************************* ***********
    }


    私有类TestClient的扩展WebViewClient {
        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            Log.i(TAG,处理的WebView URL点击......);
            view.loadUrl(URL);
            返回true;



        }





        公共无效onPageFinished(web视图查看,字符串URL){
            Log.i(TAG,加载完成URL:+网址);
            如果(progressBar.isShowing()){
                progressBar.dismiss();
            }

            如果(url.startsWith(电子邮件地址:)|| url.startsWith(全球环境展望)||
                    url.startsWith(电话:)){
                                                意向意图=新的意图
                    (Intent.ACTION_VIEW,Uri.parse(URL));
                                                startActivity(意向);
                    }
        }
    }



        公共布尔的onkeydown(INT键code,KeyEvent的事件){
            如果((钥匙code == KeyEvent.KEY code_BACK)及和放大器; webview.canGoBack()){
                webview.goBack();
                返回true;
            }
                如果(键code == KeyEvent.KEY code_SEARCH){

                    意图Z =新的意图(这一点,Search.class);
                    startActivity(Z);

                  }
            返回super.onKeyDown(键code,事件);
            }




        公共布尔onCreateOptionsMenu(功能菜单){
            super.onCreateOptionsMenu(菜单);
            MenuInflater充气= getMenuInflater();
            inflater.inflate(R.menu.menu,菜单);
            返回true;
        }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
            开关(item.getItemId()){
            案例R.id.home:
                意图M =新的意图(这一点,Home.class);
                startActivity(米);
                返回true;

            案例R.id.refresh:
                webview.reload();
                Toast.makeText(这一点,刷新......,Toast.LENGTH_SHORT).show();
                返回true;


    }
    返回false;
        }

    公共无效的onStop()
    {
       super.onStop();

       CookieSyncManager.getInstance()同步()。

        }




}
 

解决方案

有(至少)为您的问题的两种可行方案:

  1. 的不同版本不同的操作系统 - 创建不同的构建软件针对不同的操作系统。这是一种常见的做法,很多开源项目提供了不同的构建对于Linux,在Windows,Mac OS。你的情况类同,你可以创建不同的构建为Android 1.5,2.0,...

  2. 一个版本为所有的Andr​​oid OS - 如果你能找到一种方法来检测操作系统在运行时的版本,那么你可以重新设计你的code到不同的操作系统上运行。您可以为Android 1.6和2.0添加特殊类和确保,仅加载正确的类。

一个简单的例子您code:

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    // ...

    进度= ProgressDialog.show(Places.this,,加载页面...);

    如果(isGeolocationAvailable()){
       Android20Util.enableGeolocation(web视图);
    }
}
 

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

 公共静态无效enableGeolocation(的WebView的WebView){
    webview.getSettings()setGeolocationEnabled(真)。
    webview.setWebChromeClient(新GeoClient());
}
 

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

        }




} 

解决方案

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

这篇关于如何忽略某些方法和code一类?基于SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆