First Android App part 2: AndroidManifest.xml
หนนี้จะกล่าวถึงไฟล์สำคัญชื่อ AndroidManifest.xml ที่ควบคุม component ต่างๆ ไม่ว่าจะเป็นประดา activity, content provider, service, receivers ตลอดจนรายการ permission ทั้งหมด
เนื้อหาก่อนหน้านี้
AndroidManifest.xml
ที่ Android Studio กดปุ่ม shift ติดกันสองครั้ง dialog การค้นหาไฟล์จะปรากฏขึ้น ให้พิมพ์ไปบางส่วน เช่น AndroidMan ดังรูป
แล้วเลือกไฟล์ AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pros.firstapp"><application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FirstApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></manifest>
หากอยากรู้ว่าไฟล์นี้สถิตอยู่ตรงไหนให้กดไปที่เครื่องหมาย Select Opened File
ทีนี้มาดูการกำหนดค่าและความหมายที่ควรทราบในไฟล์นี้กัน
package="com.pros.firstapp"
คือชื่อ package ที่เราได้ตั้งไว้ตอน configuration โปรเจกต์
android:allowBackup="true"
ค่านี้จะมีใน Android 6.0 หรือ API level 23 ขึ้นไป เป็นการสำรองข้อมูลผู้ใช้จากแอปต่างๆที่กำหนดไว้ในยามที่เปลี่ยนอุปกรณ์
android:icon="@mipmap/ic_launcher"
ic_launcher คือไอคอนตัวเรียกใช้แอป เป็นการบอกว่ามันอยู่ตรงไหนผ่านสัญลักษณ์ @mipmap ซึ่งเป็นไฟล์ XML ดูรูป
android:label="@string/app_name"
มันคือชื่อของแอปเราที่ได้ระบุไว้ตอนสร้างโปรเจกต์ ถูกอ้างถึงผ่านสัญลักษณ์ @string ในรูปแบบเป็นไฟล์ XML
android:roundIcon="@mipmap/ic_launcher_round"
เหมือนกับ @mipmap ข้างต้น แต่เป็นไอคอนแบบขอบกลมขึ้นอยู่กับอุปกรณ์นั้นๆของผู้ใช้
android:supportsRtl="true"
รองรับการแสดงผลภาษาแบบ right-to-left
android:theme="@style/Theme.FirstApp"
กำหนด theme ของแอป เช่น text styles หรือ colors
<activity android:name=".MainActivity"> … </activity>
เป็นการระบุว่าหน้าจอ (screen) แรกเริ่มที่จะเห็นจะมาจาก MainActivity
Permissions
สิทธิ์การใช้งานเพื่อเข้าถึง resources หรือขออนุญาตก่อนการใช้งานจากผู้ใช้ ถูกกำหนดใน Android 6.0 หรือ API level 23 ขึ้นไป แบ่งออกเป็น 3 ระดับ ดังนี้
- Normal ไม่ต้องขออนุญาตจากผู้ใช้ เช่น Wi-Fi, Internet หรือ Bluetooth
- Signature สิทธิ์ที่แชร์ข้อมูลร่วมกันในกลุ่มเดียวกัน โดยต้องลงนามในใบรับรองเดียวกัน
- Dangerous ต้องขออนุญาตจากผู้ใช้เพราะกระทบกับความเป็นส่วนตัวของผู้ใช้ เช่น ส่ง SMS, เข้าถึง location, การเขียนและอ่าน filesystem หรือ contacts
Let’s Go!
เมื่อทราบอย่างนี้แล้วเดี๋ยวเราจะเพิ่ม permission การใช้ Internet เพื่อใช้งาน WebView กันครับ
เปิดไฟล์ MainActivity.kt แก้ไขโค้ดในเมธอด onCreate
ดังนี้
super.onCreate(savedInstanceState)
val webView = WebView(this)
webView.settings.javaScriptEnabled = true
setContentView(webView)
webView.loadUrl("https://outlook.live.com/")
โดยคลาส WebView ได้ import มาจาก
import android.webkit.WebView
ภาพรวม
ผลลัพธ์
เพื่อนๆคงรู้ว่าเป็นปกติที่ web app ถูกรันด้วยภาษา JavaScript เราจึงกำหนด webView.settings.javaScriptEnabled = true
จากนั้นสั่งให้มันเปิดไปที่ https://outlook.live.com/
ทว่าผลลัพธ์กลับเปิดไม่ได้
นั่นเพราะมันขาดการกำหนด permission
เปิดไฟล์ AndroidManifest.xml แล้วเพิ่ม INTERNET
permission ดังนี้
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pros.firstapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FirstApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
รันอีกครั้ง… ผลลัพธ์