0

i am using chrome custom tabs for displaying various websites.i have added a share link button in custom tabs action bar.

        builder.setActionButton(bitmap,shareLabel,createPendingShareIntent());      

and my pendingintent function

 private PendingIntent createPendingShareIntent() {
    Intent actionIntent = new Intent(Intent.ACTION_SEND);
    actionIntent.setType("text/plain");
    actionIntent.putExtra(Intent.EXTRA_TEXT,getResources().getString(R.string.chromeextra));
    return PendingIntent.getActivity(
            getApplicationContext(), 0, actionIntent, 0);
}

now i want to change Intent.EXTRA_TEXT for sharing links depanding on which link is opened by user.

i know about PendingIntent.FLAG_UPDATE_CURRENT but i don't know how to use in this scenario.

Shubham Shukla
  • 988
  • 2
  • 13
  • 28

1 Answers1

0

After a long time i found a solution. i have done this by using broadcast receiever.

first create a custom broadcastreciever class to share link

ShareBroadcastReceiver.java

public class ShareBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String url = intent.getDataString();
    if (url != null) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT,context.getResources().getString(R.string.chromeextra)+ url);

        Intent chooserIntent = Intent.createChooser(shareIntent, "Share url");
        chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(chooserIntent);
    }
}

}

then set menu item in custom tabs builder class

  String shareLabel = getString(R.string.label_action_share);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
        android.R.drawable.ic_menu_share);

//Create a PendingIntent to your BroadCastReceiver implementation
Intent actionIntent = new Intent(
        this.getApplicationContext(), ShareBroadcastReceiver.class);
PendingIntent pendingIntent = 
        PendingIntent.getBroadcast(getApplicationContext(), 0, actionIntent, 0);            

//Set the pendingIntent as the action to be performed when the button is clicked.            
intentBuilder.setActionButton(icon, shareLabel, pendingIntent);
Shubham Shukla
  • 988
  • 2
  • 13
  • 28