我们已经对 Android 应用程式的写法有了概观的认识。可是我们还没有触及 Android 平台的特别之处:整合网路的应用。
在上一章中,我们学到如何使用对话框,与如何在对话框下添加按钮,以回到原画面。在这一章裡,我们来為我们的应用程式添加一个简单的网路功能:在上一章实做的「openOptionsDialog」对话框函式中,新添一个「连线到首页」的按钮。
我们先把「openOptionsDialog」函式中使用到的字串增加到「res/values/string.xml」裡。因此完整的「res/values/string.xml」档案如下:
1<?xml version="1.0" encoding="utf-8"?>
2<resources>
3<string name="app_name">BMI</string>
4<string name="height">身高 (cm)</string>
5<string name="weight">体重 (kg)</string>
6<string name="bmi_btn">计算 BMI 值</string>
7<string name="bmi_result">你的 BMI 值是 </string>
8
9<string name="about_title">关於 Android BMI</string>
10<string name="about_msg">Android BMI Calc 0.6\n
11作者 gasolin\n
12gasolin+android [at] gmail.com</string>
13<string name="ok_label">确认</string>
14<string name="homepage_label">首页</string>
15 </resources>
增加了「连线到首页」按钮,完整「openOptionsDialog」函式的新版程式码如下:
1private void openOptionsDialog() {
2new AlertDialog.Builder(this)
3.setTitle(R.string.about_title)
4.setMessage(R.string.about_msg)
5.setPositiveButton(R.string.ok_label,
6new DialogInterface.OnClickListener(){
7public void onClick(
8DialogInterface dialoginterface, int i){
9}
10})
11.setNegativeButton(R.string.homepage_label,
12new DialogInterface.OnClickListener(){
13public void onClick(
14DialogInterface dialoginterface, int i){
15//go to url
16Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
17Intent intent = new Intent(Intent.ACTION_VIEW, uri);
18startActivity(intent);
19}
20})
21.show();
22}
讲解http://androidbmi.googlecode.com/」。
在上一章「openOptionsDialog」函式的基础上,我们在函式中添加了一个「setNegativeButton」方法,以提供另一个「NegativeButton」按钮。
.setNegativeButton(R.string.homepage_label,
new DialogInterface.OnClickListener(){
public void onClick(
DialogInterface dialoginterface, int i){
.....
}
})
与上一章我们将 DialogInterface 中的内容空白不同的是,我们為这个按钮添加了连线到特定网址(首页)的「动作」,当使用者按下「首页」按钮后,程式会开啟瀏览器,并连线到本专案的首页「
要完成整个连线的「动作」只需要三行程式码:
//go to url 这是註解
Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
以下是分行详细的讲解:
Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
建立一个 Uri 实体,裡面包含我们要连到的网址「http://androidbmi.googlecode.com/」。
在我们第一次在程式码中加入「Uri」时叙述时,「Uri」下方会出现红色的线,表示「Uri」可能是个需要由外部导入(import)的函式或类别。在「Eclispe」开发环境中,我们可以使用「ctrl-shift-O」(Windows)或「cmd-shift-O」(Mac)来自动在程式开头的地方导入「android.net.Uri」函式库。
startActivity(intent);
透过「startActivity」 函式,Android 系统即根据收到不同 「意图」(Intent) 的动作和内容,开啟对应的新页面或新程式。
在 Android 平台上,各个 Activity 之间的呼叫与交流都要透过「startActivity」 一类的函式来互动。「startActivity」 一类的函式中,最重要需传入的内容就是「意图」(Intent) 。因此我们在后面会进一步阐述「Intent」与「Activity」之间的关系。
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
在这行中,我们建立一个「意图」(Intent) 实体,并传入这个意图的「动作」与「内容」。
Intent 的格式如下:
Intent intent = new Intent(动作, 内容);
我们所建立「意图」(Intent) 中,所传入的「动作」是「Intent.ACTION_VIEW」。「Intent.ACTION_VIEW」是 Android 内建的「动作」之一。在 Eclipse 编辑画面中输入「Intent.」时,Eclipse 编辑器会弹出可输入的建议动作选单,我们可以透过这个选单,了解可使用的各种 Intent 内建动作。
「Intent.ACTION_VIEW」这个动作的意义為:依所提供内容的不同,开啟对应的程式以检视内容资料。我们可以把这个动作想成在一般桌面系统上,开啟一个已知的档案类型档案(比如说一张照片),作业系统会用你预设的应用程式(比如说某看图软体)开啟这个档案。本例中提供了「Uri」网址类型的内容给「Intent.ACTION_VIEW」这个动作,所得到的结果,就是开啟瀏览器并前往「http://androidbmi.googlecode.com/」 页面。
再做好一点
前面我们看到 Uri 实体的定义方法:
Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
看到 Uri.parse() 中,有一个固定的网址,你应该也会想把它抽取出来,丢到 Resource 中统一管理。那麼我们把程式改写,首先把网址抽取出来,放到「res/values/string.xml」档案中。 「res/values/string.xml」档案更新如下:
...
<string name="homepage_label">首页</string>
<string name="homepage_uri">http://androidbmi.googlecode.com/</string>
我们把 Uri.parse() 函式修改,传入资源识别符号.
Uri uri = Uri.parse(R.string.homepage_uri);
糟了,Eclipse 上出现了红线,表示在我们的程式码裡,有什麼地方弄错了。 因為我们只修改了 Uri.parse 传入的内容,我们可以很确定是我们传入的内容错了。 我们重新输入「Uri.」,利用 Eclipse 的自动提示功能,看看 parse 函式到底接受那些类别的参数。 原来,Uri.parse() 函式并不接受资源识别符号型态的输入。 这麼一来,我们就得自行根据资源识别符号,来取得资源识别符号所代表的文字叙述内容。 真正能执行的程式码如下:
Uri uri = Uri.parse(getString(R.string.homepage_uri));
在程式中,我们可以使用「android.content.Context」类别中的「getString」 函式(或是getText),来取得资源识别符号对应的文字。