Giới thiệu
Trong hệ điều hành Android có những Activity mà mọi app có thể sử dụng ví dụ như mở trình duyệt, call phone, sending email, google play store. Vậy các mở và thao tác với những activity này như thế nào thì bài viết hôm nay mình sẽ cùng các bạn tìm hiểm.
Intent Explicit và Intent Implicit
Trước khi đi vào sử dụng những Implicit mình sẽ phân biệt cho các biệt Intent Explicit và Intent Implicit khác nhau như thế nào.
Intent Explicit
Intent Explicit hay dịch ra là Intent tường minh và nó như thế nào nhỉ. Các bạn còn nhớ khi chúng ta sử muốn chuyển từ activity này sang activity thông qua phương thức startActivity và khởi tạo đối tượng intent như sau:
1 |
Intent intent = new Intent(MainActivity.this, AboutActivity.class); |
Các bạn thấy rằng chúng ta phải truyền vào hai activity là activity hiện tại và activity bạn muốn chuyển qua. Thì đó chính là Intent Explicit.
Intent Implicit
Intent Implicit (Intent không tường minh) cũng giống như Intend Emplicit nhưng Intent Implicit không cần phải chỉ định rõ ràng những activity mà bạn chỉ cần truyền vào một biến cờ tương ứng với những activity mà bạn sử dụng. Ví dụ như
1 |
Intent callIntent = new Intent(Intent.ACTION_CALL); |
Trong bài viết này mình sẽ hướng dẫn nhưng Intent Implicit thường xuyên sử dụng trong app ví dụ như intent callphone, send video, share content, open brower, google store play.
Intent Implicit thường xuyên sử dụng
Phone Call
1 2 3 4 5 |
Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:0377778888")); if (callIntent.resolveActivity(getPackageManager()) != null) { startActivity(callIntent); } |
Send Email
1 2 3 4 5 6 7 8 |
Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("plain/text"); intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" }); intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); intent.putExtra(Intent.EXTRA_TEXT, "mail body"); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(intent, "")); } |
Send Gmail
1 2 3 4 5 6 7 8 9 10 11 12 |
String uriText = "mailto:youremail@gmail.com" + "?subject=" + Uri.encode("some subject text here") + "&body=" + Uri.encode("some text here"); Uri uri = Uri.parse(uriText); Intent sendIntent = new Intent(Intent.ACTION_SENDTO); sendIntent.setData(uri); if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(sendIntent, "Send email")); } |
Launch Website
1 2 3 4 |
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); if (browserIntent.resolveActivity(getPackageManager()) != null) { startActivity(browserIntent); } |
Open Google Play Store
1 2 3 4 5 |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } |
Compose SMS
1 2 3 4 5 6 7 8 |
Uri smsUri = Uri.parse("tel:" + to); Intent intent = new Intent(Intent.ACTION_VIEW, smsUri); intent.putExtra("address", to); intent.putExtra("sms_body", message); intent.setType("vnd.android-dir/mms-sms");//here setType will set the previous data null. if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } |
Google Maps
1 2 3 4 5 6 7 8 9 10 |
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); String data = String.format("geo:%s,%s", latitude, longitude); if (zoomLevel != null) { data = String.format("%s?z=%s", data, zoomLevel); } intent.setData(Uri.parse(data)); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } |
Capture Photo
1 2 3 4 5 6 |
Uri uri = Uri.fromFile(new File(file)); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } |
Sharing Content
Hình ảnh hoặc các dữ liệu nhị phân
1 2 3 4 5 6 7 |
Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("image/jpg"); Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg")); sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString()); if (sharingIntent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(sharingIntent, "Share image using")); } |
Share HTML
1 2 3 4 5 6 |
Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/html"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text shared.</p>")); if (sharingIntent.resolveActivity(getPackageManager()) != null) { startActivity(Intent.createChooser(sharingIntent,"Share using")); } |