News & Events
Bài 13: Filters trong Android
- 18/07/2015
- Posted by: Bùi Đạt
- Category: Hướng dẫn học lập trình mobile app
Trong bài 12, bạn đã biết mục đích của một đối tượng Intent, là một cấu trúc dữ liệu thụ động tổ chức một mô tả trừu tượng của một hoạt động được thực hiện, mô tả trừu tượng của một hoạt động được thực hiện. Intent được sử dụng với startActivity để khởi động một hoạt động, broadcastIntent để gửi nó vào bất kỳ thành phần BroadcastReceiver quan tâm, và startService (Intent) hoặc bindService (Intent, ServiceConnection, int) để giao tiếp với một background Service. Bài này chúng ta sẽ tiếp bài 12 và tìm hiểu về Filters trong lập trình android.
1. Intent Filters
Bạn đã thấy cách một Intent đã được sử dụng để gọi một hoạt động khác. Hệ điều hành Android sử dụng các bộ lọc để xác định tập hợp các hoạt động, dịch vụ và thu phát sóng có thể xử lý các Intent với sự giúp đỡ của bộ quy định của Activities, Services, và Broadcast receivers dữ liệu liên kết với một Intent. Bạn sẽ sử dụng <intent-filter> phần tử trong tập tin manifest vào danh sách xuống hành động, danh mục và các loại dữ liệu liên quan đến bất kỳ activity, service, và broadcast receiver.
Sau đây là một ví dụ về một phần của tập tin AndroidManifest.xml để xác định một hoạt động com.example.My Application.CustomActivity có thể được gọi bởi một trong hai hành động đã đề cập, một trong những thể loại, và một dữ liệu
1 2 3 4 5 6 7 8 9 10 11 | <activity android:name=".CustomActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="com.example.My Application.LAUNCH" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity> |
Một khi hoạt động này được xác định cùng với các bộ lọc đã đề cập ở trên, các hoạt động khác sẽ có thể gọi hoạt động này bằng một trong hai android.intent.action.VIEW, hoặc sử dụng các hành động com.example.My Application.LAUNCH cung cấp danh mục là android.intent .category.DEFAULT.
Các <data> chỉ rõ kiểu dữ liệu dự kiến của hoạt động được gọi là và cho ví dụ trên hoạt động tùy chỉnh của chúng tôi hy vọng các dữ liệu để bắt đầu với “http: //”
Có thể có một tình huống mà một ý định có thể vượt qua các bộ lọc của nhiều hơn một hoạt động hay dịch vụ, người sử dụng có thể được yêu cầu đó thành phần để kích hoạt. Một ngoại lệ xảy ra nếu không có mục tiêu có thể được tìm thấy.
– Một bộ lọc <intent-filter> có thể liệt kê nhiều hơn một hành động như trên nhưng danh sách này không thể để trống; một bộ lọc phải chứa ít nhất một yếu tố <action>, nếu không nó sẽ chặn tất cả ý nghĩa. Nếu có nhiều hơn một hành động được đề cập sau đó Android sẽ cố gắng để phù hợp với một trong những hành động đã đề cập trước khi gọi hoạt động này.
– Một bộ lọc <intent-filter> có thể liệt kê không, một hoặc nhiều hơn một loại. Nếu không có thể loại được đề cập sau đó Android luôn luôn vượt qua bài kiểm tra này, nhưng nếu có nhiều hơn một loại được đề cập sau đó cho một ý định để vượt qua các thử nghiệm thể loại, mỗi thể loại trong các đối tượng Intent phải phù hợp với một danh mục trong các bộ lọc.
– Mỗi <data> yếu tố có thể chỉ định một URI và một kiểu dữ liệu (MIME là loại phương tiện truyền thông). Có thuộc tính riêng biệt như chương trình, máy chủ, cổng và đường dẫn cho mỗi phần của URI. Một đối tượng Intent mà chứa cả một URI và một kiểu dữ liệu qua các kiểu dữ liệu là một phần của bài thi chỉ nếu loại của nó phù hợp với một kiểu liệt kê trong bộ lọc.
2. Ví dụ
Bước | Mô tả |
1 | Bạn sẽ sử dụng android studio để tạo ra một ứng dụng Android và đặt tên nó là ứng dụng của tôi dưới một gói com.example.saira_000.myapplication . Trong khi tạo dự án này, chắc chắn bạn Target SDK và biên dịch với các phiên bản mới nhất của Android SDK sử dụng các cấp cao hơn của các API. |
2 | Sửa file src/Main/Java/MainActivity.java và thêm mã để xác định ba người nghe tương ứng với ba nút được định nghĩa trong file layout. |
3 | Add a new src/Main/Java/CustomActivity.java file to have one custom activity which will be invoked by different intents. |
4 | Modify layout XML file res/layout/activity_main.xml to add three buttons in linear layout. |
5 | Add one layout XML file res/layout/custom_view.xml to add a simple <TextView> to show the passed data through intent. |
6 | Modify AndroidManifest.xml to add <intent-filter> to define rules for your intent to invoke custom activity. |
7 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
Sau đây là nội dung của tập tin hoạt động chính đã sửa đổi src/MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | package com.example.saira_000.myapplication; import android.content.Intent; import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button b1,b2,b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.example.com")); startActivity(i); } }); b2=(Button)findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("com.example.My Application.LAUNCH",Uri.parse("http://www.example.com")); startActivity(i); } }); b3=(Button)findViewById(R.id.button3); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("com.example.My Application.LAUNCH",Uri.parse("https://www.example.com")); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
Sau đây là nội dung của tập tin hoạt động chính sửa đổi src/com.example.My Application/CustomActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.saira_000.myapplication; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.TextView; public class CustomActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); TextView label = (TextView) findViewById(R.id.show_data); Uri url = getIntent().getData(); label.setText(url.toString()); } } |
Sau đây sẽ là nội dung của res/layout/activity_main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Intent Example" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/abc" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageButton" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start browsing with view action" android:id="@+id/button" android:layout_alignTop="@+id/editText" android:layout_alignRight="@+id/textView1" android:layout_alignEnd="@+id/textView1" android:layout_alignLeft="@+id/imageButton" android:layout_alignStart="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start browsing with launch action" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exceptional condition" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> </RelativeLayout> |
Sau đây sẽ là nội dung của file res/layout/custom_view.xml
1 2 3 4 5 6 7 8 9 10 11 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/show_data" android:layout_width="fill_parent" android:layout_height="400dp"/> </LinearLayout> |
Sau đây sẽ là nội dung của res/values/strings.xml để xác định hai hằng số mới
1 2 3 4 5 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> <string name="action_settings">Settings</string> </resources> |
Sau đây là nội dung mặc định của AndroidManifest.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.My Application" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.saira_000.myapplication" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.saira_000.myapplication.CustomActivity" <android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="com.example.saira_000.myapplication.LAUNCH" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity> </application> </manifest> |
Bây giờ chúng ta hãy bắt đầu với nút đầu tiên “Start Browser with VIEW Action“. Dưới đây, chúng tôi đã xác định hoạt động tùy chỉnh của chúng tôi với một bộ lọc “android.intent.action.VIEW“, và đã có một hoạt động mặc định chống lại hành động VIEW xác định bởi Android được tung ra trình duyệt web, màn hình android Vì vậy sau hai tùy chọn để lựa chọn hoạt động các bạn muốn khởi động.
Bây giờ nếu bạn chọn Browser, sau đó Android sẽ khởi động trình duyệt web và các trang web example.com mở, nhưng nếu bạn chọn tùy chọn IndentDemo sau đó Android sẽ ra mắt CustomActivity mà không làm gì nhưng chỉ nắm bắt dữ liệu thông qua và hiển thị trong một lần xem văn bản như sau
Bây giờ quay trở lại sử dụng nút quay lại và bấm vào nút “Start Browser with LAUNCH Action” , ở đây Android áp dụng bộ lọc để lựa chọn xác định hoạt động và nó chỉ đơn giản là khởi động hoạt động tùy chỉnh của bạn
Một lần nữa, quay trở lại sử dụng nút quay lại và bấm vào “Exception Condition” nút, ở đây Android sẽ cố gắng để tìm ra một bộ lọc hợp lệ cho mục đích đưa ra nhưng nó không tìm thấy một hoạt động có giá trị được xác định bởi vì thời gian này, chúng tôi đã sử dụng dữ liệu như https thay vì http mặc dù chúng tôi đang đưa ra một hành động đúng, vì vậy Android đặt ra một ngoại lệ màn hình và các chương trình sau đây
Trung tâm đào tạo lap trinh php và hoc photoshop từ A->Z tại Vietpro !
Trả lời Hủy
Website này sử dụng Akismet để hạn chế spam. Tìm hiểu bình luận của bạn được duyệt như thế nào.