Implicit Intent is not known which app or android component is a target. You have to specifics action in intent, that can invoke any android app on the device able to perform the action. Why Implicit Intent is required? Sometimes your app can’t perform particular actions. At that time you can use implicit intent with action and other apps that can able to this action will show to the user by the android system. If there are multiple apps that can able to do, then the pick-up option will show. This example we will write in Kotlin.
In Android, there are 2 types of Intent.
- Implicit Intent
- Explicit Intent
On this tutorial, you can read about all basic of about Android Intent and Types of Intent.
Note: If your app supports some action like open images then you have to implement Intent Filter in your app component.
Here is Some actions can do with
- Call
- Call Log
- Contact
- Browser
- Gallery
- Camera
- Show alarm
And so many more actions can do with Implicit Intent, you don’t need to write all app ability to do things. Some awesome specifics app already in the device to handle those actions. Like call app, there is so many call apps, and Android also his own. So you don’t need to build your on calling app component, just pass the intent and let handle to another app.
Official doc (more options) : https://developer.android.com/guide/components/intents-common
Let’s Build an App for Implicit intent in kotlin :
In this example, we are sharing some example of Implicit intent with kotlin language.
Step 1. Create a new project “Build Your First Android App in Kotlin“
Step 2. Add the string in res/values/strings.xml
<resources> <string name="app_name">Implicit Intent Example</string> <string name="phone_number">Phone Number</string> <string name="call">Call</string> <string name="call_logs">Call Logs</string> <string name="contact">Contact</string> <string name="web_browser">Web Browser</string> <string name="gallery">Gallery</string> <string name="camera">Camera</string> <string name="alarm">Alarm</string> </resources>
Step 3. Open an “activity_main.xml” resource file
add buttons or add the following code
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:isScrollContainer="true"> <EditText android:id="@+id/et_number" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:hint="@string/phone_number" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btn_call" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/call" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/et_number" /> <Button android:id="@+id/btn_call_log" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/call_logs" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_call" /> <Button android:id="@+id/btn_contact" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/contact" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_call_log" /> <Button android:id="@+id/btn_browser" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/web_browser" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_contact" /> <Button android:id="@+id/btn_gallery" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/gallery" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_browser" /> <Button android:id="@+id/btn_camera" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/camera" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_gallery" /> <Button android:id="@+id/btn_alarm" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/alarm" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_camera" /> </android.support.constraint.ConstraintLayout>
Step 4. Open an “ActivityMain.kt” class file
and add the following code
*App permission is required for some actions.
package `in`.eyehunt.implicitintentexample import android.Manifest import android.annotation.SuppressLint import android.content.Intent import android.content.pm.PackageManager import android.net.Uri import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.provider.AlarmClock import android.provider.CallLog import android.provider.CallLog.* import android.provider.ContactsContract import android.provider.MediaStore import android.support.v4.app.ActivityCompat import android.support.v4.content.ContextCompat import android.util.Log import android.view.View import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { val CALL_REQUEST_CODE = 101 @SuppressLint("MissingPermission") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setupPermissions() // Calling using intent btn_call.setOnClickListener(View.OnClickListener { val intent = Intent(Intent.ACTION_CALL) intent.data = Uri.parse("tel:" + et_number.getText()) startActivity(intent) }) // Call log btn_call_log.setOnClickListener(View.OnClickListener { val intent = Intent(Intent.ACTION_VIEW) intent.type = Calls.CONTENT_TYPE startActivity(intent) }) //Contact btn_contact.setOnClickListener(View.OnClickListener { val intent = Intent(Intent.ACTION_PICK) intent.type = ContactsContract.Contacts.CONTENT_TYPE startActivity(intent) }) // browser btn_browser.setOnClickListener(View.OnClickListener { val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse("http://tutorial.eyehunts.com/") startActivity(intent) }) // Gallery btn_gallery.setOnClickListener(View.OnClickListener { val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse("content://media/external/images/media/") startActivity(intent) }) // camera btn_camera.setOnClickListener(View.OnClickListener { val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) startActivity(intent) }) // alarm btn_alarm.setOnClickListener(View.OnClickListener { val intent = Intent(AlarmClock.ACTION_SHOW_ALARMS) startActivity(intent) }) } private fun setupPermissions() { val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) if (permission != PackageManager.PERMISSION_GRANTED) { Log.i("noone", "Permission to Call has denied") makeRequest() } } private fun makeRequest() { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CALL_PHONE), CALL_REQUEST_CODE) } }
Step 5. Add Call permission in “AndroidManifest.xml“
<uses-permission android:name="android.permission.CALL_PHONE" />
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.eyehunt.implicitintentexample"> <uses-permission android:name="android.permission.CALL_PHONE" /> <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/AppTheme"> <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>
Step 6. Run the application, in an emulator or On your Android device
Output screenshot Implicit Intent kotlin example in Android :
Video Output
Download source code Implicit Intent kotlin example
https://github.com/EyeHunts/ImplicitIntentExample
Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)
MinSdkVersion=”15″
TargetSdkVersion=”27″
Coding in Kotlin
you nedd to add this permission in AndroidManifest.xml
If I want an intent to open a video from JSON, what do I have to do?