Implicit Intent trong Android
Implicit Intent: là loại Intent có các Action được Android xây dựng sẵn, nó không chỉ rõ các Component xử lý (các class xử lý) mà nó sẽ cung cấp cho hệ điều hành một loạt các thông tin yêu cầu sau đó hệ điều hành sẽ đối chiếu xem trong hệ thống có bao nhiêu phần mềm khác có thể đáp ứng xử lý yêu cầu này rồi hiện ra một Dialog chứa tên của các phần mềm có thể xử lý để người dùng chọn. Nếu các phần mềm có thể xử lý thông tin chỉ có 1 thì nó sẽ được chạy luôn không qua bước hiển thị chọn phần mềm.
Ví dụ, đoạn code dưới đây sẽ yêu cầu hệ thống android để xem một trang web.Tất cả các trình duyệt web phải được đăng ký tương ứng vào dữ liệu intent thông qua bộ lọc intent.
Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.hiepsiit.com")); startActivity(intent);
Vì đây là xem một trang web nên đoạn code này ta sử dụng ActionView
Ví dụ, đoạn code sau gọi điện thoại
Uri uri = Uri.parse("tel:" + "900"); Intent intent = new Intent(Intent.ACTION_CALL, uri); // Để có thể call được thì trong AndroidMaintifest.xml phải khai báo // <uses-permission android:name="android.permission.CALL_PHONE" /> startActivity(intent);
Đoạn code này thì ta sử dụng ActionCall là vì ta sẽ mở chương trình gọi điện.
Các bạn có thể xem các Action mà Android định nghĩa sẵn ở đây:
http://developer.android.com/reference/android/content/Intent.html
Các bạn thấy hai đoạn code trên có sử dụng đối tượng Uri, đối tượng này được dùng để nạp dữ liệu theo chuẩn RFC 3986.
Bảng dưới đây liệt kê một số định dạng và action tương ứng đã được định nghĩa sẵn:
Ví dụ: Trong ví dụ này chúng ta sẽ làm ứng dụng gồm có 1 EditText và 1 Button. Khi người sử dụng gõ url vào EditText, sau đó click "Visit" sẽ mở website lấy từ EditText.Tiến hành tạo project, vào thư mục res /layout -> activity_main.xml thiết kế giao diện sau:
Bước 1: Tạo một project tên là ImplicitIntents: File->New->Android Application Project điền các thông tin ->Next ->Finish
Bước 2: Mở res -> layout -> xml (hoặc) activity_main.xml và thêm code.
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="hiepsiit.com.implicitintent.MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="54dp" android:text="Visit" /> </RelativeLayout>
Bước 3: Mở src -> package -> MainActivity.java
package hiepsiit.com.implicitintent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editText1=(EditText)findViewById(R.id.editText1); Button button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String url=editText1.getText().toString(); Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url)); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Ứng dụng này được phát triển bởi adt bundle, android 4.2 sử dụng minimum sdk 8 and target sdk 21.
Kết quả khi chạy ứng dụng
Nhập vào website và Nhấn vào nút "Visit"
Kết quả
Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com