Blog Archives

android debug vm virtual virtualbox images ova eclipse

As an alternative to the default android virtual machines that come with the Android SDK and/or Eclipse, try the images from the site below:

http://androvm.org/blog/

The format is ova, which is easily opened by virtualbox, and there only two simple things to keep in mind.

1) After adding into virtualbox, add a port forwarding rule to the first ethernet device. Even though this defaults to “not attached” I changed this to NAT and added rules for 5555, 8600 and 8601.

Make sure you do not turn off the second ethernet device as this virtual machine is configured to use both of these adapters.

From Eclipse you can change your settings (window->properties->android) to always prompt you to select a device. Run your virtual machine and go into settings and enable developer mode, then run your app in Eclipse and you should see the device in the top box of the devices window that appears.

If for any reason you can no longer see the device, go into the DDMS perspective and click on the restart adb button and you should now see it again.

That’s it! Fast, simple and better IMO than the out of the box config that comes with Eclipse for android development. Enjoy.

Advertisement

android app cannot connect to internet

To allow your custom app to connect to the internet, add the following in your AndroidManifest.xml file right before (outside) the application tag.

<uses-permission android:name="android.permission.INTERNET" />

Android Launch Browser from App to handle external URLs

Upon googling for this one, I found some good relevant links on stack overflow as well as the android webview documentation, but no singular solution that tied everything together.

First, I created a custom WebViewClient class. Note that this is optional as you can do inline class declarations in this environment.

package com.example.testapp;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;

public class MyWebViewClient extends WebViewClient {
@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null &amp;amp;&amp;amp; url.startsWith('http://192.168.1.101')) {
            return false;
        } else {
        	view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
    }
}

Then, in the main activity, use code within onCreate to support for multiple windows and to set the web view client to your own custom client.

package com.example.testapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.net.Uri;

public class MainActivity extends Activity {

	private WebView webView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	final Context context = this;
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    	webView = (WebView) findViewById(R.id.webView1);
    	webView.getSettings().setJavaScriptEnabled(true);
    	
    	webView.getSettings().setSupportMultipleWindows(true);

    	webView.setWebViewClient(new MyWebViewClient());
    	webView.loadUrl('http://192.168.1.1/login.aspx');
    }

If you want to use an inline class declaration instead of defining a seperate mywebviewclient class, you can also use something like:

    	webView.setWebViewClient(new WebViewClient() {
    	      @Override
    	      public boolean shouldOverrideUrlLoading(WebView view, String url) {
//etc, same code as above
    	      }
});

If you have not already addressed the question of handling the android back button within your webview, the android documentation has a good quick simple code reference on how to accomplish this.

Enjoy. 🙂

References
StackOverflow, http://stackoverflow.com/questions/5979361/android-open-url-in-a-new-window-without-leaving-app

StackOverflow, http://stackoverflow.com/questions/7028258/launch-browser-from-within-app-how-do-you-get-back-to-the-app

Android Documentation, http://developer.android.com/reference/android/webkit/WebView.html