文章目錄
- 零、學習目標
- 一、Intent十二種常用功能
- 1、瀏覽網頁
- 2、瀏覽地圖
- 3、調出撥打電話界面
- 4、直接撥打電話
- 5、卸載應用程式
- 6、安裝應用程式
- 7、播放存盤卡音樂
- 8、呼叫發郵件
- 9、發郵件
- 10、發短信
- 11、直接發短信
- 12、發彩信
- 二、案例演示 - 通過意圖瀏覽網頁
- (一)運行效果
- (二)實作步驟
- 1、創建安卓應用【BrowseWebByIntent】
- 2、將背景圖片拷貝到drawable目錄
- 3、主布局資源檔案activity_main.xml
- 4、字串資源檔案strings.xml
- 5、主界面類 - MainActivity
- 6、啟動應用,查看效果
- (三)課堂練習:瀏覽用戶指定網址
- 三、WebView概述
- 四、案例演示 - 通過WebView瀏覽網頁
- (一)運行效果
- (二)實作步驟
- 1、創建安卓應用【BrowseWebByWebView】
- 2、將背景圖片拷貝到drawable目錄
- 3、主布局資源檔案activity_main.xml
- 4、字串資源檔案strings.xml
- 5、在專案清單檔案里授權訪問因特網
- 6、主界面類 - MainActivity
- 7、啟動應用,查看效果
- 五、案例演示 - 網頁與安卓通信
- (一)運行效果
- (二)實作步驟
- 1、創建安卓應用【WebCommunicatesWithAnroid】
- 2、主布局資源檔案activity_main.xml
- 3、字串資源檔案strings.xml
- 4、在assets目錄里創建網頁檔案 - test.html
- 5、在專案清單檔案里授權訪問因特網
- 6、主界面類 - MainActivity
- 7、啟動應用,查看效果
- (三)課堂練習 - 實作運算式計算器
零、學習目標
- 掌握通過意圖瀏覽網頁
- 掌握通過WebView瀏覽網頁
- 掌握如何實作網頁與安卓通信
一、Intent十二種常用功能
1、瀏覽網頁
Uri uri = Uri.parse("http://www.lzy.edu.cn");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
2、瀏覽地圖
Uri uri = Uri.parse("geo:68.500511,-66.0068787");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
3、調出撥打電話界面
Uri uri = Uri.parse("tel:1008");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
4、直接撥打電話
Uri uri = Uri.parse("tel:1008");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
5、卸載應用程式
Uri uri = Uri.fromParts("package", "net.hw.music", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
6、安裝應用程式
Uri uri = Uri.fromParts("package", "net.hw.music", null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
startActivity(intent);
7、播放存盤卡音樂
Uri uri = Uri.parse("file:///sdcard/music.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
8、呼叫發郵件
Uri uri = Uri.parse("mailto:maths007@163.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
9、發郵件
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = {"maths007@163.com"};
String[] ccs = {"maths007@163.com"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.setType("message/rfc008");
Intent.createChooser(intent, "Choose Email Client");
10、發短信
Uri uri = Uri.parse("smsto:10000");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra("sms_body", "Hi, are you free tonight?");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
11、直接發短信
Uri uri = Uri.parse("smsto://10000");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hi, are you free now?");
startActivity(intent);
12、發彩信
Uri uri = Uri.parse("content://media/external/images/media/1");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "hi, are you ok?");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
二、案例演示 - 通過意圖瀏覽網頁
利用Intent設定Action(Intent.ACTION_VIEW)與Data,可以實作瀏覽網頁的功能,
(一)運行效果

(二)實作步驟
1、創建安卓應用【BrowseWebByIntent】


2、將背景圖片拷貝到drawable目錄

3、主布局資源檔案activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<Button
android:id="@+id/btnBrowseWeb"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doBrowseWeb"
android:text="@string/browse_web"
android:textSize="20sp" />
</LinearLayout>
4、字串資源檔案strings.xml

<resources>
<string name="app_name">通過意圖瀏覽網頁</string>
<string name="browse_web">瀏覽網頁</string>
</resources>
5、主界面類 - MainActivity

package net.hw.browse_web_by_intent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
/**
* 功能:通過意圖瀏覽網頁
* 作者:華衛
* 日期:2020年12月31日
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局檔案設定用戶界面
setContentView(R.layout.activity_main);
}
/**
* 瀏覽網頁
*
* @param view
*/
public void doBrowseWeb(View view) {
// 決議網址,定義統一資源識別符號
Uri uri = Uri.parse("http://www.lzy.edu.cn");
// 創建瀏覽網頁的意圖
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// 按意圖啟動視窗,瀏覽網頁
startActivity(intent);
}
}
6、啟動應用,查看效果

(三)課堂練習:瀏覽用戶指定網址
- 在編輯框里輸入網址

- 單擊【前往】按鈕,瀏覽指定網址

三、WebView概述
WebView是安卓里一個非常重要的控制元件,顯示和渲染網頁,可與頁面JavaScript互動,實作混合開發,內核是webkit引擎,4.4版本之后,直接使用Chrome作為內置網頁瀏覽器,使用WebView之前,不要忘記在專案清單檔案中授權訪問因特網,
四、案例演示 - 通過WebView瀏覽網頁
(一)運行效果

(二)實作步驟
1、創建安卓應用【BrowseWebByWebView】


2、將背景圖片拷貝到drawable目錄

3、主布局資源檔案activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:hint="@string/input_url"
android:textSize="20sp" />
<Button
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/go"
android:textSize="20sp" />
</LinearLayout>
<WebView
android:id="@+id/wvWebPage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4、字串資源檔案strings.xml

<resources>
<string name="app_name">通過WebView瀏覽網頁</string>
<string name="input_url">請輸入網址</string>
<string name="go">前往</string>
</resources>
5、在專案清單檔案里授權訪問因特網

6、主界面類 - MainActivity

package net.hw.browse_web_by_webview;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText edtURL; // 網址編輯框
private Button btnGo; // 前往按鈕
private WebView wvWebPage; // 網頁視圖
private String strURL; // 網址字串
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局資源檔案設定用戶界面
setContentView(R.layout.activity_main);
// 通過資源識別符號獲取控制元件實體
edtURL = findViewById(R.id.edtUrl);
btnGo = findViewById(R.id.btnGo);
wvWebPage = findViewById(R.id.wvWebPage);
// 給按鈕注冊監聽器,撰寫事件處理代碼
btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 獲得文本框里輸入的網址
strURL = edtURL.getText().toString();
// 判斷網址是否以"http://或https://"打頭
if (checkURL(strURL)) {
openBrowser(strURL);
} else {
Toast.makeText(MainActivity.this, "網址必須以“http://或https://”打頭!", Toast.LENGTH_LONG).show();
}
}
});
// 給文本框注冊監聽器,撰寫事件處理代碼
edtURL.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// 獲得文本框里輸入的網址
strURL = edtURL.getText().toString();
// 判斷網址是否以"http://或https://"打頭
if (checkURL(strURL)) {
openBrowser(strURL);
return true; // 事件處理完畢,不再往后傳播
} else {
Toast.makeText(MainActivity.this, "網址必須以“http://或https://”打頭!", Toast.LENGTH_LONG).show();
}
}
return false;
}
});
}
/**
* 讓瀏覽器引擎加載網址,顯示網頁
*/
protected void openBrowser(String strUrl) {
// 設定JavaScript可用
wvWebPage.getSettings().setJavaScriptEnabled(true);
// 創建并設定web客戶端
wvWebPage.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true; // 回傳true在webview里顯示網頁,否則呼叫安卓自帶瀏覽器或第三方瀏覽器顯示網頁
}
});
// 加載網址,顯示網頁內容
wvWebPage.loadUrl(strURL);
}
/**
* 功能:檢查網址是否以“http://或https://”打頭
*
* @param url 網址
* @return 合法網址回傳真,否則回傳假
*/
private boolean checkURL(String url) {
if (url.length() > 7) {
if (url.substring(0, 7).equalsIgnoreCase("http://")) {
return true;
}
} else if (url.length() > 8) {
if (url.substring(0, 8).equalsIgnoreCase("https://")) {
return true;
}
}
return false;
}
}
7、啟動應用,查看效果

五、案例演示 - 網頁與安卓通信
(一)運行效果


(二)實作步驟
1、創建安卓應用【WebCommunicatesWithAnroid】


2、主布局資源檔案activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/wvWebPage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffff00"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btnCallJavaScriptFromAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/call_javascript_from_android"
android:textSize="20sp" />
<TextView
android:id="@+id/tvMessageFromWebPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
3、字串資源檔案strings.xml

<resources>
<string name="app_name">網頁與安卓通信</string>
<string name="call_javascript_from_android">在安卓里呼叫JavaScript</string>
</resources>
4、在assets目錄里創建網頁檔案 - test.html

<html>
<head>
<meta charset="gbk">
<script type="text/javascript">
function callJS(arg){
document.getElementById('message_from_android').innerHTML=arg;
}
</script>
</head>
<body style="text-align:center">
<h1>網頁視圖(WebView)</h1>
<p>
<a href="#" onclick="window.alert('來自網頁的警告資訊:2021元旦快樂!')" style="font-size: 25px">顯示JavaScript警告框</a>
</p>
<p>
<a href="#" onclick="window.android.callAndroid('來自網頁的資訊:你好!')" style="font-size: 25px">在網頁里呼叫安卓</a>
</p>
<p id="message_from_android" style="font-size:20px; color: #ff00ff"></p>
</body>
</html>
5、在專案清單檔案里授權訪問因特網

6、主界面類 - MainActivity

package net.hw.web_android;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
/**
* 功能:網頁與安卓通信
* 作者:華衛
* 日期:2021年01月01日
*/
public class MainActivity extends AppCompatActivity {
/**
* 網頁視圖
*/
private WebView wvWebPage;
/**
* 按鈕:在安卓里呼叫JavaScript
*/
private Button btnCallJavaScriptFromAndroid;
/**
* 標簽:顯示來自網頁的訊息
*/
private TextView tvMessageFromWebPage;
/**
* 訊息處理器,用于瀏覽器執行緒與主執行緒之間的轉換
*/
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局資源檔案設定用戶界面
setContentView(R.layout.activity_main);
// 通過資源識別符號獲取控制元件實體
wvWebPage = findViewById(R.id.wvWebPage);
btnCallJavaScriptFromAndroid = findViewById(R.id.btnCallJavaScriptFromAndroid);
tvMessageFromWebPage = findViewById(R.id.tvMessageFromWebPage);
// 實體化訊息處理器
handler = new Handler();
// 設定JavaScript可用
wvWebPage.getSettings().setJavaScriptEnabled(true);
// 利用wvWebPage加載本地頁面
wvWebPage.loadUrl("file:///android_asset/test.html");
// 設定網頁瀏覽器客戶端,能監聽到網頁彈出警告框
wvWebPage.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
/* 創建一個安卓警告對話框 */
// 創建警告對話框生成器
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// 設定對話框標題
builder.setTitle("提示");
// 設定對話框圖示
builder.setIcon(R.mipmap.ic_launcher);
// 設定對話框正文
builder.setMessage(message);
// 設定確定按鈕
builder.setPositiveButton("確定", null);
// 根據設定創建警告對話框
AlertDialog dialog = builder.create();
// 顯示警告對話框
dialog.show();
// 處理用戶確認操作
result.confirm();
// 事件處理完畢
return true;
}
});
// 給按鈕注冊監聽器,撰寫事件處理代碼
btnCallJavaScriptFromAndroid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 利用wvWebPage呼叫JavaScript函式
wvWebPage.loadUrl("javascript:callJS('來自安卓的資訊:你好!')");
}
});
/*
* 溝通網頁與安卓的橋梁類,實體化后可供JavaScript呼叫,
* 當JavaScript呼叫AndroidBridge物件的callAndroid方法時,
* 安卓就會創建一個Runnable物件,并且把它放到handler的運行佇列中,
* 這樣主執行緒一有機會就會呼叫run()方法,修改標簽內容,
*/
class AndroidBridge {
@JavascriptInterface
public void callAndroid(final String arg) {
handler.post(new Runnable() {
@Override
public void run() {
tvMessageFromWebPage.setText(arg); // 設定標簽內容
}
});
}
}
// 實體化AndroidBridge,名為android供JavaScript呼叫,
wvWebPage.addJavascriptInterface(new AndroidBridge(), "android");
}
}
7、啟動應用,查看效果


(三)課堂練習 - 實作運算式計算器




轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/243607.html
標籤:其他
