Android Implicit Intent

 

  • Android Implicit Intent is used to invoke inbuilt system components or invoke components from another applications.
  • Android Implicit Intent don’t directly specify android component which is to be called. Instead Implicit Intent just specify the action to be performed & what matter is to be passed to that component like URL, Map Lat-Long., Ph No., etc.
  • Example of Implicit Intent  – invoking Webview to load a webpage. Here we will specify Action i.e. to view webpage, irrespective of which browser opens that page. You might have seen OS gives a prompt to choose from multiple available browsers to user. So developer doesn’t specify which Activity is to be launched, it is up to user to select from available options.

In this tutorial we will cover Implicit Intent, keeping Explicit Intent for our forth coming Tutorial.



Download
Source Code : Implicit Intent.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Implicit Intent which you might have downloaded from above or you can follow the scripts below by copying it to implement  in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/ImplicitIntent/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

1. MainActivity.java

Explaining above Code :

  • Line 12 We have declared 3 string variable containing a Website URL, Ph Number & Latitude-Longitude coordinates. ( Here we have prefixed Ph No with ‘tel:‘ for using it to place call and Latitude-Longitude with ‘geo:‘ to plot the coordinates over the map )
  • Line 23 We have our button-click handler for 1st button, which will be called to invoke Webview (Android’s Default Web-Browser).
  • Line 26 We make an instance of Intent class and pass 2 parameters:  Intent.ACTION_VIEW  specifying this implicit intent which will launch a visual view item of Android OS  &  Uri.Parse(website) will invoke Webview(with URL stored inside ‘website’ variable) and convert our URL in usable format by the system.
  • Line 28 At this line our current MainActivity.java will be paused & sent in background, after that it launches our Webview.
  • Line 33 Whenever we click on 2nd button this handler method will be called.
  • Line 36 Over here you will see that syntax is same as for Webview, but our variable ‘number‘ contains “tel:” as a prefix (Line-12), using this system identifies and invokes Dialer Key-Pad instead of Webview.
  • Line 38 At this line our current MainActivity.java will be paused & sent in background, after that it launches our Dialler Key-pad.
  • Line 43 Whenever we click on 3rd button this handler method will be called.
  • Line 46 Over here you will see Implicit Intent with two parameters, android.content.Intent.ACTION_CALL this will invoke the default application which we use to call any Number and Uri.parse(number) will provide Ph No. to that application.
  • Line 48 At this line our current MainActivity.java will be paused & sent in background, after that it places a new call.( Make sure that you change the variable number, since this will actually place a call and you may bear charges as per your carrier. But if you are using AVD you don’t need to worry )
  • Line 53 Whenever we click on 4th button this handler method will be called.
  • Line 57 Over here you will see Implicit Intent with two parameters, android.content.Intent.ACTION_VIEW this will invoke the default application used to plot a map and Uri.parse(lat_long) will provide co-ordinates to that application based on which you will see your map. ( prefix  ‘geo:‘ will help system to detect co-ordinates to plot on map )
  • Line 59 At this line our current MainActivity.java will be paused & sent in background, after that  Google Map will be shown over the screen. If you are using AVD then your application will Crash devoid of Google maps in it. Try it from actual Mobile / Tablet. 

 

 

2. activity_main.xml : Layout file

  • Inside our activity_main.xml we have added  4 Button which will invoke  methods containing Implicit Intent.

 

3. AndroidManifest.xml

  •  Line 8 In order to use Android functionalities we need to provide permissions to our Application. In order to place call directly from our App we have added ‘android.permission.CALL_PHONE‘ . Without these permissions your App. will crash.
  • Line 9 While using Webview to see a webpage we need to get access to internet for that we add ‘android.permission.INTERNET‘.

 

 

 

Final Output :

Android Implicit Intent

Fig(1) MainActivity Screen

Android Implicit Intent

Fig(2)  WebView opened using Implicit Intent

 

 

 

 

Android Implicit Intent

Fig(3) Dialer Key-pad

Android Implicit Intent

Fig(4) Calling a Number using Implicit Intent

 

 

 

 

Android Implicit Intent

Fig(5) Plotting Geo. coordinates on map

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Trouble-Shooting :

  • For App Crash
    Check that you have added permissions in Manifest.xml. Few OS versions will not permit direct calling functionality for security reasons. Also check GPS should be ON, and emulators may not support Geo-locations.

 

 

 

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.