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...

Solution of Image not Loaded or Auto Suggestions not working in Android Studio

Sometimes we face issue when some images are not loaded or showing in Android Studio . It shows that image not loaded try to open it externally to fix format problem .It cause when some gradle files are corrupted. also if you try auto suggestions not working in xml layout files like  you can try Invalidate Caches and Restart in Android Studio's File Menu option but you will get no result . So here are some steps to follow to remove this type of problem: Step 1: Close your Android Studio Completely. Go to your User Folder - on Windows 7/8 this would be: [SYSDRIVE]:\Users[your username] (ex. C:\Users\DroidMedium\) there you find a folder with name .AndroidStudio3.5  *( i am using Android Studio 3.5.1 version your version may be different according to your Android Studio version) open this folder now you will find two folders Go to System folder  C:\Users\DroidMedium\.AndroidStudio3.5\System\ Step 2: Now under System Folder you will find Caches folder Delete the Folder Com...

How to Implement Item Click Interface in Android?

In Android development, creating interactive lists is essential to most apps. Often, these lists include items that users can click on to trigger actions such as opening a new screen or displaying more information. In this tutorial, we’ll walk through how to implement an item click interface in Android using Java and XML. By the end of this guide, you’ll know how to create a RecyclerView with an item click listener that responds to user taps. This approach is widely used in modern Android development because RecyclerView is both flexible and efficient. Step 1: Set Up Your Android Project First, create a new Android project in Android Studio. Make sure to choose Java as the programming language. Step 2: Add Dependencies Make sure that you have the required dependencies for RecyclerView in your build.gradle file. If not, add them like this: dependencies {     implementation 'androidx.recyclerview:recyclerview:1.2.1' } Sync the project after adding the dependency. Step 3: Define ...