Skip to main content

Android Implicit Intent Examples

In Android development we often use intents to use Activity Communications and other Intent Services. As per Documentation "An Intent is a messaging object you can use to request an action from another app component ". 

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.

It is generally used with startActivity() method to invoke activity, broadcast receivers etc.

The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.

Android intents are mainly used to:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.

There are two types of intents in android:

1) Implicit Intent:

Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
 Intent intent=new Intent(Intent.ACTION_VIEW);   
 intent.setData(Uri.parse("https://www.droidmedium.blogspot.com"));   
 startActivity(intent);   
2) Explicit Intent:

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
 Intent i = new Intent(getApplicationContext(), OtherActivity.class);   
 startActivity(i);   

So Above is brief introduction of intent. Lets use Practical example of different types of explicit intnt examples:

Intent to Open Web Browser for specific URL:
 String url = "http://www.droidmedium.blogspot.com";  
 Intent i = new Intent(Intent.ACTION_VIEW);  
 i.setData(Uri.parse(url));  
 startActivity(i);  

Intent to Open Email Client:
  Intent mEmail=new Intent(Intent.ACTION_SENDTO);  
       mEmail.setData(Uri.parse("mailto: example@gmail.com"));  
       mEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"});  
       mEmail.putExtra(Intent.EXTRA_SUBJECT,"Your Subject Here");  
       mEmail.putExtra(Intent.EXTRA_TEXT, "Your Message Here");  
      startActivity(Intent.createChooser(mEmail, "Choose An Email Client to send your feedback!!"));  

Intent to Open Camera:
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
   File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");  
   intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));  
   imageUri = Uri.fromFile(photo);  
   startActivityForResult(intent, TAKE_PICTURE);  

Intent to Open Photo Gallery:
 Intent intent = new  Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
 startActivityForResult(intent, 2);  

Intent to Open Contact:
 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);   
 startActivityForResult(intent, 1);   

Intent to Open PDF File:
 File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");  
 Intent intent = new Intent(Intent.ACTION_VIEW);  
 intent.setDataAndType(Uri.fromFile(file), "application/pdf");  
 intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
 startActivity(intent);  

Intent to Open Google Map:
 Intent intent = new Intent(android.content.Intent.ACTION_VIEW,   
  Uri.parse("http://maps.google.com/maps?saddr=xx.xxxx,yy.yyyy&daddr=xx.xxxxxx,yy.yyyyyy"));  
 startActivity(intent);  

Intent to Open WhatsApp:
 String contact = "+91 xxxxxxxxxx"; // use country code with your phone number  
   String url = "https://api.whatsapp.com/send?phone=" + contact;  
   try {  
      PackageManager pm = context.getPackageManager();  
      pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);  
      Intent i = new Intent(Intent.ACTION_VIEW);  
      i.setData(Uri.parse(url));  
      startActivity(i);                
   } catch (PackageManager.NameNotFoundException e) {  
   Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();  
   e.printStackTrace();  
   }  

Intent to Open Play Store:
 final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object  
 try {  
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));  
 } catch (android.content.ActivityNotFoundException anfe) {  
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));  
 }  

Intent to Open Another App:
 // Use package name which we want to check  
     boolean isAppInstalled = appInstalledOrNot("com.check.application");   
     if(isAppInstalled) {  
       //This intent will help you to launch if the package is already installed  
       Intent LaunchIntent = getPackageManager()  
         .getLaunchIntentForPackage("com.check.application");  
       startActivity(LaunchIntent);  
       Log.i("Application is already installed.");      
     } else {  
       // Do whatever we want to do if application not installed  
       // For example, Redirect to play store  
       Log.i("Application is not currently installed.");  
     }  
   }  
   private boolean appInstalledOrNot(String uri) {  
     PackageManager pm = getPackageManager();  
     try {  
       pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);  
       return true;  
     } catch (PackageManager.NameNotFoundException e) {  
     }  
     return false;  
   }  

Intent to Open Apk Installer:
  Uri uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",new File(location+"Example.apk"));  // location is path of file location  
     Intent intent = new Intent(Intent.ACTION_VIEW);  
     intent.setDataAndType(uri, "application/vnd.android.package-archive");  
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
     getContext().startActivity(intent);  

Intent to Open SMS App:
 Intent intent = new Intent(Intent.ACTION_MAIN);  
 intent.addCategory(Intent.CATEGORY_APP_MESSAGING);  
 startActivity(intent);  

Intent to Ope UPI Apps:
  String payeeAddress = "xxxxxxxxx@upi"; // replace x with payee UPI Address   
   String payeeName = "Droid Medium";  // replace with payee Name   
   String transactionNote = "Test for UPI Deeplinking"; //can pass random txn Id   
   String amount = "1";   // Amount here    
   String currencyUnit = "INR";   
   String transid ="ABX1234"; // Random transaction Id    
 Uri uri = Uri.parse("upi://pay?pa="+payeeAddress   
       +"&pn="+payeeName   
       +"&tn="+transactionNote   
       + "&mc=0000"    
       +"&tid=" + transid    
       + "&tr=" + transid   
       +"&am="+amount   
       +"&mam=" + null        
       +"&cu="+currencyUnit);   
      Log.d(TAG, "onClick: uri: "+uri);   
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
      startActivityForResult(intent,1);   

Thats all for now .So using these example we can use implicit Intent for our Android App.


References:  StackOverflow & JavaTpoint

Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl

How to open pdf file url using webview in Android

 Sometimes we have requirement of showing pdf file in our Android Application. Although there are many third party gradle dependencies available online by which you can show your pdf file easily but one of the major drawback of using these libraries are they will increase you apk size .In some cases they will increase apk size upto ~14 MB. Alternatively you can access and show pdf file using assets and from storage options. This can take less size than any third party library. Here we use pdf using url and if you directly open your pdf file url in webview you will get no result because it will start downoad if you open this link in any web browser.  So in this article we will learn how we can open our pdf url using webview and without using any third party library. So lets Start step by Step:- Lets Suppose you have a pdf file url something like   http://yourwebsite.com/files/mydocumentfile.pdf Step 1: So we you this pdf file url to embed in our webview .First of all you have to host yo

How to use UPI Deep Linking in Android

 In this Article we will discuss about the UPI Deep linking. By this Deep linking we can perform UPI Payments through intent .Basically it open the All UPI supported Application in our mobile then user choose their preference and initiate the Payment and through this we get the payment credentials like payment id ,transaction id ,etc. without any payment gateway. So lets start with what is UPI? Unified Payments Interface(UPI) is an instant real-time payment system developed by National Payments Corporation of India facilitating inter-bank transactions. The interface is regulated by the Reserve Bank of India and works by instantly transferring funds between two bank accounts on a mobile platform. (from Wikipedia) Coding part: package com.example.droidmedium; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Butt