share intent text in android studio using kotlin

你说的曾经没有我的故事 提交于 2019-11-30 15:21:25

Try this :- Use Intent.EXTRA_STREAM only when you have to send Binary data like images , if you want to sent text use Intent.EXTRA_TEXT

    val shareIntent = Intent()
    shareIntent.action = Intent.ACTION_SEND
    shareIntent.type="text/plain"
    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    startActivity(Intent.createChooser(shareIntent,getString(R.string.send_to)))

Just use

context.startActivity

Insted of

startActivity

You can certainly go to definition of the Start Activity and view the overloads as you are not passing one of the acceptable parameters.

What I like to do, in order to have navigation code all over the place, is to create an IntentFactory like this:

public class IntentFactory {

/**
 *
 * @param context
 * @return intent
 */
public static Intent getLoginIntent(Context context, boolean launchedFromNotification, boolean isApproveNotify, String idOfDetailToOpen, NewsModel notificationModel){
    Intent intent = new Intent(context, LoginActivity.class);
    intent.putExtra(Globals.INTENT_KEYS.KEY_FROM_BADGE_ACCESS, launchedFromNotification);
    intent.putExtra(Globals.INTENT_KEYS.KEY_ID_OF_DETAIL_TO_OPEN, idOfDetailToOpen);
    intent.putExtra(Globals.INTENT_KEYS.KEY_IS_APPROVE_NOTIFY, isApproveNotify);
    intent.putExtra(Globals.INTENT_KEYS.KEY_ALERT_NOTIFICATION_FOR_APPROVAL, notificationModel);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    return intent;

}
/**
 *
 * @param filePath
 * @param subject
 * @param body
 * @return
 */
public static Intent getSendImageIntent(String filePath, String subject, String body){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/jpg");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, body);
    return Intent.createChooser(intent, "Share File");

}
/**
 *
 * @param toEmailAddresses
 * @param subject
 * @param body
 * @param uris
 * @return
 */
public static Intent getEmailIntent(String[] toEmailAddresses, String subject, String body, ArrayList<Uri> uris) {
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, toEmailAddresses);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, body);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    if(uris != null) {
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

    }

    return Intent.createChooser(intent, "Send mail...");

}
/**
 * Used to launch to app details screen for toggling of permissions or other things
 * @param context
 * @return
 */
public static Intent getShowAppDetailSettingsIntent(Context context){
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setData(Uri.parse("package:" + context.getPackageName()));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    return intent;

}

}

Then you can easily manage using your StartActivity calls with

      String email = SharedPreferencesManager.getInstance(getContext()).getSecuredPreference(REMEMBER_ME_USER_KEY);
    String subject = getString(R.string.message) + " " + model.getFirstName() + " " + model.getLastName();
    String body = getString(R.string.subject) + model.getFirstName() + " " + model.getLastName() + "\n" + model.getShootKeyUrl();

    startActivity(IntentFactory.getEmailIntent(new String[]{email}, subject, body, null));

So if you align the proper parameters you will be good, but I would still recommend extracting it for ease of updating to a factory like this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!