【问题标题】:Share Intent text using Kotlin on Android在 Android 上使用 Kotlin 共享 Intent 文本
【发布时间】:2018-05-16 15:42:07
【问题描述】:

我想在我的 CardView 中使用 kotlin 的 share Intent 共享文本,但 kotlin 代码的最后一行有问题 代码

 val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND
            shareIntent.putExtra(Intent.EXTRA_STREAM, "ali")
            shareIntent.type = "text/plain"
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))

这是代码中的问题

startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))

请帮帮我

看图片来了解我

图片

https://ibb.co/jQwYXw

https://ibb.co/id0tXw

https://ibb.co/fbCU5G

适配器完整代码

class MyAdapter(context: Context, listItem: ArrayList<com.EliteTeam.comedytaste.Model.Movie>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

var Context = context
var movieList = listItem;
var layoutInflator = LayoutInflater.from(context)

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {

    var inflateView = layoutInflator.inflate(R.layout.single_item, parent, false)

    return MyViewHolder(inflateView)
}

override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {

    holder?.moviewTitle?.text = movieList[position].name
    holder?.movieDescription!!.text=   movieList[position].description
    //holder!!.cardImageView!!.background=   movieList[position].image

    holder?.onclick(Context, position)
    holder!!.cardImageView.setBackgroundResource(movieList[position].image)
}

override fun getItemCount(): Int {

    return movieList.size
}

class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

    var moviewTitle: TextView = itemView?.findViewById(R.id.movieTitleTextView)!!
    var movieDescription: TextView = itemView!!.findViewById(R.id.movieDescriptionTextView)
    var cardImageView: CardView = itemView!!.findViewById(R.id.imageCard)
    var share: ImageButton = itemView!!.findViewById(R.id.share)

    fun onclick(context: Context, position: Int) {
        cardImageView.setOnClickListener {

    }

       share.setOnClickListener {

           val shareIntent = Intent()
         shareIntent.action = Intent.ACTION_SEND
           shareIntent.putExtra(Intent.EXTRA_TEXT, "ali")
          shareIntent.type = "text/plain"


           startActivity(Intent.createChooser(shareIntent,"send to"))

}} }}

【问题讨论】:

  • 究竟是什么问题???
  • 这段代码在我把它翻译成kotlin之前在java中工作,当我把它翻译成kotlin时它不起作用
  • “没用” ..请详细说明

标签: android kotlin android-intent share-intent


【解决方案1】:

试试这个:- Intent.EXTRA_STREAM 仅在必须发送二进制数据(如图像)时使用,如果要发送文本使用 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)))

如果在适配器中使用此代码,那么最后一行应该是 context.startActivity(Intent.createChooser(shareIntent,getString(R.string.send_to)))

【讨论】:

  • 不工作 createChooser(shareIntent,getString(R.string.send_to)) 但是当在 send _to 之后添加逗号时它告诉我添加发件人这意味着什么 createChooser(shareIntent,getString(R.string .send_to),)
  • 我认为你缺少右括号)而不是逗号
  • 正如我上面解释的那样,只需在现有代码中将 extra.stream 更改为 extra.text 即可
  • 有帮助吗?
  • 是的,但正如我在第一个命令中所说,它在 kotlin 中缺少一些呼叫发件人
【解决方案2】:

随便用

context.startActivity

插入

startActivity

【讨论】:

  • 是的,这是评论中相同的解决方案 Suraj Nair 谢谢
【解决方案3】:

您可以直接使用这些功能:

fun showSharingDialogAsKotlin(text: String) {
    val intent = Intent()
    intent.action = Intent.ACTION_SEND
    intent.type = "text/plain"
    intent.putExtra(Intent.EXTRA_TEXT, text)
    startActivity(Intent.createChooser(intent, "Share with:"))
}

另外,如果你想展示你的图片并且你有一个链接:

fun showSharingDialogAsKotlinWithURL(text: String, url: String) {
    val intent = Intent()
    intent.action = Intent.ACTION_SEND
    intent.type = "text/plain"
    intent.putExtra(Intent.EXTRA_TEXT, "$text: $url")
    startActivity(Intent.createChooser(intent, "Share with:"))
}

【讨论】:

    【解决方案4】:

    您当然可以转到 Start Activity 的定义并查看重载,因为您没有传递可接受的参数之一。

    为了让导航代码遍布各处,我喜欢做的是像这样创建一个 IntentFactory:

    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;
    
    }
    
    }
    

    然后您可以轻松管理使用您的 StartActivity 调用

          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));
    

    所以如果你调整正确的参数你会很好,但我仍然建议提取它以便于更新到这样的工厂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多