codeofaninja
website

Android Intents Tutorial to Share Your Social Media Links

Photo of Mike Dalisay
Modified Tuesday, December 10, 2013
by - @ninjazhai
The following Android intents tutorial will help increase your app's Facebook likes or twitter followers. Almost every app has their own social media accounts or links that you can like, follow, +1, or even send an email to. There's always an app that can connect you to that product or service.



Now, from that app, a user must be able to view your accounts effortlessly and in a standard, native and beautiful way. Don't just throw a browser with a link to a social media account, impress your users by opening it in the native app! The most commonly used social media apps are Facebook, Twitter and Google Plus.

Those are covered in this post, but with the additional Browser and GMail intents! For this code's demo, see our Android Intents video demo below.



By the way, the app on the demo above is this blog's official browser app. You might want to download it on Google Play. I didn't have enough time to develop that app due to my busy schedule. But it is working fine, try it out!

1.0 Facebook Intent in Android


Here's how my app show that Facebook page.

Step 1: Go to the FB page and get the Facebook page name using the URL, in my case, it is "CodeOfANinja"


You can also get the Facebook page ID using the graph API URL, for example


...and see the "id": "107786425949934" on that JSON string.

Step 2: Use the method below on your Listener, just replace the Facebook page name or ID. You can also use some parameters on this method if you're opening multiple Facebook pages.

public void openFacebookPage() {
    
    Intent i = null;
    
    try {
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
        
        // replace 107786425949934 with your page ID
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/107786425949934"));
        
    } catch (Exception e) {
        
        // replace CodeOfANinja with your page name
        i =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/CodeOfANinja"));
    }
    
    checkIfAppExists(i, "Facebook");
    
}

2.0 Google Plus Intent in Android


Calling the official Google Plus app to show the Google+ page of your customers would be great. That's one feature I already worked with and now I'm ready to show you the working code I used.

Here's how I did it:

Step 1: Go to the Google+ Page and copy the page ID. In my case, it is: 101266899643014043497


Step 2: Add the following code in your file or utility class.

public void openGooglePlusPage(String googlePlusId){
    
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setClassName("com.google.android.apps.plus", "com.google.android.apps.plus.phone.UrlGatewayActivity");
    i.putExtra("customAppUri", googlePlusId);
    startActivity(i);
    
    checkIfAppExists(i, "Google Plus");
}

How to use? Just call the function with the ID you got in step 1.

openGooglePlusPage("101266899643014043497");

3.0 Twitter Intent in Android


Here's how my app show that Twitter page.

Step 1: Get the Twitter page username, in my case, it is "ninjazhai"


Step 2: Add the following method on your project or utility class.


public void openTwitterPage(String username){
    
    Intent i = null;
    
    try {
    
        // use the twitter app
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + username));
        
    }catch (Exception e) {
        
        // try to use other intent
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + username)); 
    }
    
    checkIfAppExists(i, "Twitter");
}

How to use the method above? Remember the twitter username you got on step 1 and put code below on your click listener:


openTwitterPage("ninjazhai");

4.0 Browser Intent in Android


This time we are going to take a look at the browser intent filter in android. I used this one when I want my users have the ability to view the subscribe form of this blog. This code basically answers the question "How do I show a web page from my app to a web browser?"

Here's the working code I used. Just follow this two step guide.

Step 1: Of course, prepare the URL you wanted to be shown. In my case, it was:


Step 2: Add this method to your JAVA file or utility class.

public void openUrl_InBrowser(String url){
    
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    
    checkIfAppExists(i, "Web Browser");
    
}

How to use? easy, you can do it this way.

openUrl_InBrowser("http://feedburner.google.com/fb/a/mailverify?uri=TheCodeOfANinja");

5.0 Gmail Intent in Android


You can use this code when you want your users to have the ability to send email from your app,  for example is the app feature where the users can send feedback, comments or suggestions directly to any given email address.

This code will open the Gmail App with the help of android intent filter.

Now you can add this code to your project or utility class.

public void openGmailApp(String[] emailAddresses, String subject, String text){
    
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setType("plain/text");
    i.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
    i.putExtra(Intent.EXTRA_EMAIL, emailAddresses);
    i.putExtra(Intent.EXTRA_SUBJECT, subject);
    i.putExtra(Intent.EXTRA_TEXT, text);
    checkIfAppExists(i, "Gmail");
    
}

And you can use the method above this way:

// you can try to add more email in this array: new String[] { "ninjazhai30@gmail.com", "email2@yahoo.com" }
// if you're going to send the message to multiple people
            
openGmailApp(
    new String[] { "ninjazhai30@gmail.com" }, 
    "Feedback for The Code of a Ninja Programming Blog", 
    "Sent using The Code of a Ninja Android App:\n\n"
);

6.0 How to Check If An App Is Installed in Android?


Before a specific app will run, we have to verify if that app exists or installed in the device. This will enable us to tell the user that a certain app must be installed before using a feature. This prevents showing an error  or force close message, so it is good for the user experience. The method below will show us the way.

Checking if an app is installed in the Android device is required if you are going to start another applications's intent. There are other ways to do this but the code below is what's working for me.

I used and tested this code when I wanted to start an intent to some apps like:
  • Facebook
  • Twitter
  • Google Plus
  • Browser
  • Gmail
// method to check whether an app exists or not
public void checkIfAppExists(Intent appIntent, String appName){
    
    if (appIntent.resolveActivity(context.getPackageManager()) != null) {
        
        // start the activity if the app exists in the system
        startActivity(appIntent);
        
    } else {
        
        // tell the user the app does not exist
        Toast.makeText(context, appName + " app does not exist!", Toast.LENGTH_LONG).show();
    }
}

Sample Usage: Below is the code I used when I want to open my google plus page with the Google+ official android app.

Intent i = new Intent(Intent.ACTION_VIEW);
i.setClassName("com.google.android.apps.plus", "com.google.android.apps.plus.phone.UrlGatewayActivity");
i.putExtra("customAppUri", "101266899643014043497");
startActivity(i);

checkIfAppExists(i, "Google Plus");

You can read more about Android intents here.
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
 
 
Fundamentals
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.