I want to use Android Intent.ACTION_SEND for quickly sharing something. So I got a sharing list like this:
But I want to share different content for each action, such as:
If sharing by Email/Gmail, content should be "Share by email".
If sharing by Facebook, content should be "Share by Facebook".
So, is it possible to do that?
The system will always present the chooser dialog even if the user has chosen a default one. If your intent created by Intent. createChooser doesn't match any activity, the system will still present a dialog with the specified title and an error message No application can perform this action .
To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.
FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.
An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when your app cannot perform the action, but other apps probably can and you'd like the user to pick which app to use.
You can't get such information.
Unless you create your own implementation of the dialog for the activity selection.
To create such dialog you need to use PackageManager
and its queryIntentActivities()
function. The function returns List<ResolveInfo>
.
ResolveInfo
contains some information about an activity (resolveInfo.activityInfo.packageName
), and with the help of PackageManager
you can get other information (useful for displaying the activity in the dialog) - application icon drawable, application label, ... .
Display the results in a list in a dialog (or in an activity styled as a dialog). When an item is clicked create new Intent.ACTION_SEND, add the content you want and add the package of the selected activity (intent.setPackage(pkgName)
).
There is not direct method to access such kind of information....
Step 1: Inside your code first of all you need to declare an adapter which will contain your custom view of list to be shared on...
//sharing implementation
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
// what type of data needs to be send by sharing
sharingIntent.setType("text/plain");
// package names
PackageManager pm = getPackageManager();
// list package
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
objShareIntentListAdapter = new ShareIntentListAdapter(CouponView.this,activityList.toArray());
// Create alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Share via");
builder.setAdapter(objShareIntentListAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = (ResolveInfo) objShareIntentListAdapter.getItem(item);
// if email shared by user
if(info.activityInfo.packageName.contains("Email")
|| info.activityInfo.packageName.contains("Gmail")
|| info.activityInfo.packageName.contains("Y! Mail")) {
PullShare.makeRequestEmail(COUPONID,CouponType);
}
// start respective activity
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShortDesc+" from "+BusinessName);
intent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+shortURL);
intent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+shortURL);
((Activity)context).startActivity(intent);
}// end onClick
});
AlertDialog alert = builder.create();
alert.show();
Step 2: Now you have create a layout inflater for your adapter(ShareIntentListAdapter.java)
package com.android;
import android.app.Activity;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ShareIntentListAdapter extends ArrayAdapter{
private final Activity context;
Object[] items;
public ShareIntentListAdapter(Activity context,Object[] items) {
super(context, R.layout.social_share, items);
this.context = context;
this.items = items;
}// end HomeListViewPrototype
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.social_share, null, true);
// set share name
TextView shareName = (TextView) rowView.findViewById(R.id.shareName);
// Set share image
ImageView imageShare = (ImageView) rowView.findViewById(R.id.shareImage);
// set native name of App to share
shareName.setText(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
// share native image of the App to share
imageShare.setImageDrawable(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadIcon(context.getPackageManager()));
return rowView;
}// end getView
}// end main onCreate
Step 3: Create your xml layout type to show list view in dialog box(social_share.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/categoryCell"
android:layout_width="match_parent"
android:layout_height="30dip"
android:background="@android:color/white" >
<TextView
android:id="@+id/shareName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<ImageView
android:id="@+id/shareImage"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/image_view" />
</RelativeLayout>
// vKj
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With