Mỗi ứng dụng Android đều có một tập tin tên AndroidManifest.xml, được lưu ở thư mục gốc (root) của ứng dụng. Tập tin manifest mô tả những thông tin cần thiết của ứng dụng cho hệ điều hành Android, những thông tin này hệ điều hành cần phải biết trước khi nó chạy bất kỳ chương trình nào. Những công việc mà tập tin manifest cần phải thực hiện Khai báo tên của package cho ứng dụng. Tên của package là định danh duy nhất của ứng dụng Mô tả các thành phần (component) của ứng dụng: activity, services, broadcast receivers, và content providers mà ứng dụng có. Khai báo tên các lớp mà cài đặt các thành phần này và các tác vụ mà các thành phần này có thể xử lý (ví dụ các thông điệp một Intent có thể xử lý). Những khai báo này cho phép hệ điều hành Android biết các thành phần này có thể có thể thực hiện được tác vụ gì và điều kiện để chúng có thể thực thi. Xác định tiến trình (process) sẽ chứa những thành phần (component) nào. Khai báo các quyền mà ứng dụng cần phải có để truy xuất các thành phần của API và giao tiếp với các ứng dụng khác. Khai báo các quyền mà yêu cầu ứng dụng khác cần phải có để có thể giao tiếp được với các component của ứng dụng này. Mô tả phiên bản API tối thiểu mà ứng dụng có thể chạy. Liệt kê các thư viện mà ứng dụng cần phải kết nối. 1. Cấu trúc của tập tin Manifest Phần dưới đây mô tả cấu trúc của tập tin manifest và các phần tử mà nó có thể chứa 1: <?xml version="1.0" encoding="utf-8"?> 2: 3: <manifest> 4: 5: <uses-permission /> 6: <permission /> 7: <permission-tree /> 8: <permission-group /> 9: <instrumentation /> 10: <uses-sdk /> 11: <uses-configuration /> 12: <uses-feature /> 13: <supports-screens /> 14: <compatible-screens /> 15: <supports-gl-texture /> 16: 17: <application> 18: 19: <activity> 20: <intent-filter> 21: <action /> 22: <category /> 23: <data /> 24: </intent-filter> 25: <meta-data /> 26: </activity> 27: 28: <activity-alias> 29: <intent-filter> . . . </intent-filter> 30: <meta-data /> 31: </activity-alias> 32: 33: <service> 34: <intent-filter> . . . </intent-filter> 35: <meta-data/> 36: </service> 37: 38: <receiver> 39: <intent-filter> . . . </intent-filter> 40: <meta-data /> 41: </receiver> 42: 43: <provider> 44: <grant-uri-permission /> 45: <meta-data /> 46: <path-permission /> 47: </provider> 48: 49: <uses-library /> 50: 51: </application> 52: 53: </manifest> 2. Các quy ước dành cho tập tin manifest Thẻ (element):
Thuộc tính (attribute):
Khai báo tên lớp:
Thuộc tính có nhiều giá trị (multiple values):
Tham chiếu đến giá trị trong tập tin resource khác (Resource Values):
String Values:
3. Các tính năng khác: Phần dưới đây mô tả các tính năng khác của tập tin manifest Intent Filters:
Icon và Label:
Permissions (quyền):
Library:
4. Một số tập tin AndroidManifest.xml minh họa: Tập tin AndroidManifest.xml của ứng dụng gửi/nhận SMS 1: <?xml version="1.0" encoding="utf-8"?> 2: <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3: package="com.code5s" 4: android:versionCode="1" 5: android:versionName="1.0" > 6: 7: <uses-sdk 8: android:minSdkVersion="14" 9: android:targetSdkVersion="17" /> 10: 11: <uses-permission android:name="android.permission.SEND_SMS" /> 12: <uses-permission android:name="android.permission.RECEIVE_SMS" /> 13: 14: <application 15: android:allowBackup="true" 16: android:icon="@drawable/ic_launcher" 17: android:label="@string/app_name" 18: android:theme="@style/AppTheme" > 19: <activity 20: android:name="com.code5s.MainActivity" 21: android:label="@string/app_name" > 22: <intent-filter> 23: <action android:name="android.intent.action.MAIN" /> 24: 25: <category android:name="android.intent.category.LAUNCHER" /> 26: </intent-filter> 27: </activity> 28: </application> 29: 30: </manifest> Tập tin AndroidManifest.xml của ứng dụng WifiDemo 1: <?xml version="1.0" encoding="utf-8"?> 2: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.wifidirect" 3: android:versionCode="1" android:versionName="1.0"> 4: 5: <uses-sdk android:minSdkVersion="14" /> 6: <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 7: <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 8: <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 9: <uses-permission android:name="android.permission.INTERNET" /> 10: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 11: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 12: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 13: 14: <!-- Google Play filtering --> 15: <uses-feature android:name="android.hardware.wifi.direct" android:required="true"/> 16: 17: <application 18: android:icon="@drawable/ic_launcher" 19: android:label="@string/app_name" 20: android:theme="@android:style/Theme.Holo"> 21: <activity 22: android:name=".WiFiDirectActivity" 23: android:label="@string/app_name" android:launchMode="singleTask"> 24: <intent-filter> 25: <action 26: android:name="android.intent.action.MAIN" /> 27: <category 28: android:name="android.intent.category.LAUNCHER" /> 29: </intent-filter> 30: </activity> 31: 32: <!-- Used for transferring files after a successful connection --> 33: <service android:enabled="true" android:name=".FileTransferService" /> 34: 35: </application> 36: </manifest> Tập tin AndroidManifest.xml của ứng dụng NotePad 1: <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2: package="com.example.android.notepad" > 3: 4: <uses-sdk android:minSdkVersion="11" /> 5: 6: <application android:icon="@drawable/app_notes" 7: android:label="@string/app_name" 8: > 9: <provider android:name="NotePadProvider" 10: android:authorities="com.google.provider.NotePad" 11: android:exported="false"> 12: <grant-uri-permission android:pathPattern=".*" /> 13: </provider> 14: 15: <activity android:name="NotesList" android:label="@string/title_notes_list"> 16: <intent-filter> 17: <action android:name="android.intent.action.MAIN" /> 18: <category android:name="android.intent.category.LAUNCHER" /> 19: </intent-filter> 20: <intent-filter> 21: <action android:name="android.intent.action.VIEW" /> 22: <action android:name="android.intent.action.EDIT" /> 23: <action android:name="android.intent.action.PICK" /> 24: <category android:name="android.intent.category.DEFAULT" /> 25: <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 26: </intent-filter> 27: <intent-filter> 28: <action android:name="android.intent.action.GET_CONTENT" /> 29: <category android:name="android.intent.category.DEFAULT" /> 30: <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 31: </intent-filter> 32: </activity> 33: 34: <activity android:name="NoteEditor" 35: android:theme="@android:style/Theme.Holo.Light" 36: android:screenOrientation="sensor" 37: android:configChanges="keyboardHidden|orientation" 38: > 39: <!-- This filter says that we can view or edit the data of 40: a single note --> 41: <intent-filter android:label="@string/resolve_edit"> 42: <action android:name="android.intent.action.VIEW" /> 43: <action android:name="android.intent.action.EDIT" /> 44: <action android:name="com.android.notepad.action.EDIT_NOTE" /> 45: <category android:name="android.intent.category.DEFAULT" /> 46: <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 47: </intent-filter> 48: 49: <!-- This filter says that we can create a new note inside 50: of a directory of notes. The INSERT action creates an 51: empty note; the PASTE action initializes a new note from 52: the current contents of the clipboard. --> 53: <intent-filter> 54: <action android:name="android.intent.action.INSERT" /> 55: <action android:name="android.intent.action.PASTE" /> 56: <category android:name="android.intent.category.DEFAULT" /> 57: <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 58: </intent-filter> 59: 60: </activity> 61: 62: <activity android:name="TitleEditor" 63: android:label="@string/title_edit_title" 64: android:icon="@drawable/ic_menu_edit" 65: android:theme="@android:style/Theme.Holo.Dialog" 66: android:windowSoftInputMode="stateVisible"> 67: <!-- This activity implements an alternative action that can be 68: performed on notes: editing their title. It can be used as 69: a default operation if the user invokes this action, and is 70: available as an alternative action for any note data. --> 71: <intent-filter android:label="@string/resolve_title"> 72: <!-- This is the action we perform. It is a custom action we 73: define for our application, not a generic VIEW or EDIT 74: action since we are not a general note viewer/editor. --> 75: <action android:name="com.android.notepad.action.EDIT_TITLE" /> 76: <!-- DEFAULT: execute if being directly invoked. --> 77: <category android:name="android.intent.category.DEFAULT" /> 78: <!-- ALTERNATIVE: show as an alternative action when the user is 79: working with this type of data. --> 80: <category android:name="android.intent.category.ALTERNATIVE" /> 81: <!-- SELECTED_ALTERNATIVE: show as an alternative action the user 82: can perform when selecting this type of data. --> 83: <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> 84: <!-- This is the data type we operate on. --> 85: <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 86: </intent-filter> 87: </activity> 88: 89: <activity android:name="NotesLiveFolder" android:label="@string/live_folder_name" 90: android:icon="@drawable/live_folder_notes"> 91: <intent-filter> 92: <action android:name="android.intent.action.CREATE_LIVE_FOLDER" /> 93: <category android:name="android.intent.category.DEFAULT" /> 94: </intent-filter> 95: </activity> 96: 97: </application> 98: 99: </manifest> Nguồn: http://developer.android.com Tags: androidlearningprogrammingtrainingtutorial |