Lesson 32. Writing a Simple Browser

Lesson 32. Writing a Simple Browser


In this lesson:

– writing a simple browser

In the last lesson we saw that if called Intent with action = ACTION_VIEW and given = Uri-object with httpthe address then runs browser and displays the content of the page from that http address. We can independently make the simplest browser to respond to such Intent and simply display the page. To do this, you need to configure Intent Filter and use the component WebView.

In the first window we will have a button that sends Intent. The second screen will be WebView.

Let’s create a project:

Project name: P0321_SimpleBrowser
Build Target: Android 2.3.3
Application name: SimpleBrowser
Package name: ru.startandroid.develop.p0321simplebrowser
Create Activity: MainActivity

we draw main.xml



        

Just a button on the screen

Kodyma MainActivity.java:

package ru.startandroid.develop.p0321simplebrowser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    (findViewById(R.id.btnWeb)).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ya.ru")));
      }
    });
  }
}

The code is a bit unusual. Note I am not describing a class object anywhere Button. method findViewById returns View, and that View supports the method setOnClickListenerWhich I summon. And in the method setOnClickListener I create an object that implements the interface OnClickListener and write the code in it onClick. I also create an object Intent not separately, but directly in the method startActivity. There was less code than usual. This option may be appropriate for you.

So, my on press to launch the button IntentWhich means that we want to browse the site http://www.ya.ru.

Let’s create a second Activity. First the layout file browser.xml:



        
        

On the component screen WebView.

create BrowserActivity.java:

package ru.startandroid.develop.p0321simplebrowser;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;

public class BrowserActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browser);

    WebView webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient());
    Uri data = getIntent().getData();
    webView.loadUrl(data.toString());
  }
}

we define WebView, we read given with Intent and pass line in WebView.

now we will prescribe Activity in manifestos. We will need to add to it Intent Filter, Specify in it action = ACTION_VIEW. And for given we see some parameters we use Scheme = http.

This means that Uri object in Intent must contain an http address.

Do not forget about Category = Default. Label for BrowserActivity, for example, MyBrowser.

The manifesto should also be added Uses Permission = android.permission.INTERNET on the tab Permissions. To give the application access to the Internet.

We will save everything and launch the application. We push a button and see a choice: the system offers us a choice system browser and our, just done. That is Intent found a request to view the http address on the system two Activity that in their Intent Filter have stated that they can display http addresses.

Choose our MyBrowser and see the page.

We saw that Activity in our applications can process not only our invented action, but also system. And, thus, create an alternative to system applications.

But as you know, we simply could not use WebView in our Activity and show the page. It was possible to use TextView and simply display the address from the data as text. Or an http request that would download this page and display its html content. We could generally score at http-addresses and show some picture left or just a dark screen.

That is, you can create an Intent Filter for Activity, which will tell the system that the application is capable of something, but there will be some nonsense inside the Activity. This is already a question of programming ethics, common sense and adequacy)

Full manifest code:



        
        
        
                
                        
                                
                                
                        
                
                
                        
                                
                                
                                
                        
                
        

In the next lesson:

– storing data using Preferences




Discuss in the forum [148 replies]

Leave a Comment