btn.setOnClickListener(new View.OnClickListener() {

   public void onClick(View view) {

    String _lat = lat.getText().toString();

    String _lon = lon.getText().toString();

    Uri uri=Uri.parse('geo:'+_lat+','+_lon);

    startActivity(new Intent(Intent.ACTION_VIEW, uri));

   }

  });

 }

}

The activity is not much to look at (Figure 24-1).

Figure 24-1. The LaunchDemo sample application, with a location filled in

If you fill in a location (e.g., 38.8891 latitude and -77.0492 longitude) and click the button, the resulting map is more interesting (Figure 24-2). Note that this is the built-in Android map activity — we did not create our own activity to display this map.

Figure 24-2. The map launched by LaunchDemo, showing the Lincoln Memorial in Washington DC

In a Chapter 34, you will see how you can create maps in your own activities, in case you need greater control over how the map is displayed.

Tabbed Browsing, Sort Of

One of the main features of the modern desktop Web browser is tabbed browsing, where a single browser window can show several pages split across a series of tabs. On a mobile device this may not make a lot of sense, given that you lose screen real estate for the tabs themselves.

In this book, however, we do not let little things like sensibility stop us, so let me demonstrate a tabbed browser, using TabActivity and Intents.

As you may recall from Chapter 10, a tab can have either a View or an Activity as its content. If you want to use an Activity as the content of a tab, you provide an Intent that will launch the desired Activity; Android’s tab-management framework will then pour the Activity’s user interface into the tab.

Your natural instinct might be to use an http:Uri the way we used a geo:Uri in the previous example:

Intent i = new Intent(Intent.ACTION_VIEW);

i.setData(Uri.parse ('http://commonsware.com'));

That way, you could use the built-in Browser application and get all of the features that it offers.

Alas, this does not work. You cannot host other applications’ activities in your tabs — only your own activities, for security reasons.

So, we dust off our WebView demos from Chapter 13 and use those instead, repackaged as Activities/IntentTab.

Here is the source to the main activity, the one hosting the TabView:

public class IntentTabDemo extends TabActivity {

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  TabHost host = getTabHost();

  host.addTab(host.newTabSpec ('one').setIndicator('CW')

   .setContent(new Intent(this, CWBrowser.class)));

  host.addTab(host.newTabSpec ('two').setIndicator('Android')

   .setContent(new Intent(this, AndroidBrowser.class)));

 }

}

As you can see, we are using TabActivity as the base class, and so we do not need our own layout XML — TabActivity supplies it for us. All we do is get access to the TabHost and add two tabs, each specifying an Intent that directly refers to another class. In this case, our two tabs will host a CWBrowser and an AndroidBrowser, respectively.

Those activities are simple modifications to the earlier browser demos:

public class CWBrowser extends Activity {

 WebView browser;

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  browser = new WebView(this);

  setContentView(browser);

  browser.loadUrl('http://commonsware.com');

 }

}

public class AndroidBrowser extends Activity {

 WebView browser;

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  browser = new WebView(this);

  setContentView(browser);

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату