Android sharingIntent for different actions
For the next shareIntent:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "to share this!"));
The app will then display a list of apps with sharing functionality:
I would like to ask if it is possible to code in such a way that if the client chooses for example. whatsapp or SMS will it do Action A, but if the client chooses Facebook, will it do Action B?
Thank!
source to share
What you are looking for is something like usage startActivityForResult()
and hope the target app sets the result so you can get meaningful Intent data
inonActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data){}
Nevertheless; unfortunately it is not reliable and does not work with most applications.
I've tested it with several apps including SMS, Google+, Facebook, Gmail, and ColorNote; and for all of them I get data=nulll
in mine onActivityResult
except ColorNote
that I get valid Intent data
with an action likecontent://note.socialnmobile.provider.colornote/notes/41
So it really depends on the target application and there is no other reliable method that I know of that allows your application to know which application the user has chosen to share data.
source to share
Here is an example of a sharing option I did a while ago
UPDATE:
public class CustomShareDialogActivity extends Activity {
private ArrayList< AppToSendOption > appsOptions = new ArrayList< AppToSendOption >();
@Override
protected void onCreate( Bundle arg0 ) {
super.onCreate( arg0 );
setContentView( R.layout.show_share_dialog );
final Button button = (Button)findViewById( R.id.button1 );
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View v ) {
getListOfShareApps();
showShareDialog();
}
} );
}
private void getListOfShareApps() {
if( !appsOptions.isEmpty() ){ return; }
Intent sendOption = new Intent();
sendOption.setType( "application/*" );
sendOption.setAction( Intent.ACTION_SEND_MULTIPLE );
List< ResolveInfo > ris = getPackageManager().queryIntentActivities( sendOption, 0 );
for ( ResolveInfo ri : ris ) {
Drawable icon = ri.loadIcon( getPackageManager() );
String appname = ( String ) ri.loadLabel( getPackageManager() );
String packagename = ri.activityInfo.packageName;
String classname = ri.activityInfo.name;
appsOptions.add( new AppToSendOption( icon, appname, packagename, classname ) );
}
}
private void showShareDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder( this );
ArrayAdapter< AppToSendOption > adapter01 = new SendOptionsAdapter( this, appsOptions );
builder.setTitle( "Options" )
.setSingleChoiceItems( adapter01, -1, new OnClickListener() {
@Override
public void onClick( DialogInterface dialog, int which ) {
AppToSendOption app = appsOptions.get( which );
String packagename = app.getPackagename();
String classname = app.getClassname();
// Right here, check the package name to see which app is selected, and do the appropriate
// action.
Toast.makeText( getApplicationContext(), packagename + ", " + classname, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
} ).setNegativeButton( "Cancel", null ).show();
}
private class AppToSendOption {
Drawable icon;
String appname;
String packagename;
String classname;
public AppToSendOption( Drawable icon, String appname, String packagename, String classname ) {
this.icon = icon;
this.appname = appname;
this.packagename = packagename;
this.classname = classname;
}
Drawable getIcon() {
return icon;
}
String getAppname() {
return appname;
}
String getPackagename() {
return packagename;
}
String getClassname() {
return classname;
}
}
public class SendOptionsAdapter extends ArrayAdapter< AppToSendOption > {
private List< AppToSendOption > apps;
private LayoutInflater inflater;
private static final int RESOURCE = R.layout.send_option_dialog;
class ViewHolder {
TextView text;
ImageView icon;
}
public SendOptionsAdapter( Context context, List< AppToSendOption > objects ) {
super( context, RESOURCE, objects );
inflater = LayoutInflater.from( context );
apps = objects;
}
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
ViewHolder holder;
if ( convertView == null ) {
holder = new ViewHolder();
convertView = inflater.inflate( RESOURCE, null );
holder.text = ( TextView ) convertView.findViewById( R.id.textView_appname );
holder.text.setTextColor( Color.BLACK );
holder.icon = ( ImageView ) convertView.findViewById( R.id.imageView_appicon );
holder.icon.setAdjustViewBounds( true );
holder.icon.setScaleType( ScaleType.CENTER_INSIDE );
convertView.setTag( holder );
} else {
holder = ( ViewHolder ) convertView.getTag();
}
holder.icon.setImageDrawable( apps.get( position ).getIcon() );
holder.text.setText( apps.get( position ).getAppname() );
return convertView;
}
}
}
And here is the xml file send_option_dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView_appicon"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp" >
</ImageView>
<TextView
android:id="@+id/textView_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:lines="1"
android:textColor="#fff"
android:textSize="20sp" >
</TextView>
This is not ideal, you can use DialogFragment instead, but hopefully this will give you an idea of how to create a dialog.
source to share
I would like to ask if it is possible to code in such a way that if the client chooses for example. whatsapp or SMS will it do Action A, but if the client chooses Facebook, will it do Action B?
No, for two reasons:
-
An
Intent
has only one action andstartActivity()
(ucreateChooser()
) takes only oneIntent
. -
No "whatsapp". No "SMS". No Facebook. There are applications with different package names and you cannot reliably determine if a particular package name is "whatsapp" or "SMS" or "Facebook". For example, there are hundreds, if not thousands, of SMS clients.
If you want to allow the user to share multiple types of things (like short strings or long strings), give the user a choice of what to split. If a user wants to use a short string with Facebook instead of a longer string, it is the user's decision, not yours.
source to share