Monday, May 30, 2011

Share in Android

Android provides a nice feature IntentFilter.An application is said to accept share if and only if Activity has IntentFilter ACTION_SEND in Manifest.All the apps such as Twitter,FaceBook,Mail etc.. have ACTION_SEND as Intent Filter in their Manifest Files.


       
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);  
picMessageIntent.setType("text/*"); 
picMessageIntent.putExtra(Intent.EXTRA_TEXT, "http://www.gpuri.com/images/705/70592.png");
startActivity(Intent.createChooser(picMessageIntent, "Share your picture using:"));

The above code pops a list of apps to recive share event like



Inorder to share to a particular App,First of all Know whether target application is installed in the device using packageManager.

Use following piece,

     
    Intent sendShareIntent = new Intent(Intent.ACTION_SEND);
    PackageManager pm = getPackageManager();
    List activityList = pm.queryIntentActivities(sendShareIntent, 0);
         
    for (int i = 0; i < activityList.size(); i++) { 
        ResolveInfo app = activityList.get(i);
        Log.e("###", "Name: " + app.activityInfo.name);
           
    }
            
      
So,I will check for my FaceBook app installed in my phone or not and i'll directly start facebook share activity using Intent
              
     Intent sendShareIntent = new Intent(Intent.ACTION_SEND);
     sendShareIntent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");
     sendShareIntent.setType("text/*");
     sendShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.google.com/");
     startActivity(sendShareIntent);      
  
This will activate facebook app directly if is present.It would be like


For Twitter the package is "com.twitter.android" and share class is "com.twitter.android.PostActivity".

For all the data like text,image,video just set the type of intent to "text/*" and pass the required url in putExtra(ExtraText).

Currently, In FaceBook we cannot share text.At any cost we have to send only Link URL.Otherwise it will ask for the link to post.

No comments:

Post a Comment