Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Ignore certain methods and code in a Class? Based on SDK
    primarykey
    data
    text
    <p>I was reading over <a href="http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html" rel="nofollow noreferrer">http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html</a> 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?</p> <pre><code>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) &amp;&amp; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload