是否有可能通过使用份额意图共享多个文本?(Is it possible to share multi

2019-10-23 11:12发布

我打算6共享文本信息,但它始终只显示最后一个信息,即

share.putExtra(Intent.EXTRA_TEXT, Contact); 

我还试图用一个字符串数组来存储所有的6个信息,即:

share.putExtra(Intent.EXTRA_TEXT, stringArray); 

但是,它仍然无法正常工作。 任何人都可以帮忙吗? 谢谢。

我的代码:

public class SingleJobActivity extends Activity {

// JSON node keys
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";

private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";


String PostName;
String Location;
String Salary;
String Responsibility;
String Company;
String Contact;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_job_json_parsing);

    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    PostName = in.getStringExtra(TAG_POSTNAME);
    Location = in.getStringExtra(TAG_LOCATION);
    Salary = in.getStringExtra(TAG_SALARY);

    Responsibility = in.getStringExtra(TAG_RESPONSIBILITY);
    Company = in.getStringExtra(TAG_COMPANY);
    Contact = in.getStringExtra(TAG_CONTACT);

    // Displaying all values on the screen
    TextView lblPostName = (TextView) findViewById(R.id.PostName_label);
    TextView lblLocation = (TextView) findViewById(R.id.Location_label);
    TextView lblSalary = (TextView) findViewById(R.id.Salary_label);

    TextView lblResponsibility = (TextView) findViewById(R.id.Responsibility_label);
    TextView lblCompany = (TextView) findViewById(R.id.Company_label);
    TextView lblContact = (TextView) findViewById(R.id.Contact_label);

    lblPostName.setText(PostName);
    lblLocation.setText(Location);
    lblSalary.setText(Salary);

    lblResponsibility.setText(Responsibility);
    lblCompany.setText(Company);
    lblContact.setText(Contact);

    // listeners of our  button
    View.OnClickListener handler = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.share:
                    shareTextUrl();
                    break;

            }
        }
    };

    // our button
    findViewById(R.id.share).setOnClickListener(handler);
}

// Method to share either text or URL.
private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);


    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "Job Information:");
    share.putExtra(Intent.EXTRA_TEXT, PostName);
    share.putExtra(Intent.EXTRA_TEXT, Location);
    share.putExtra(Intent.EXTRA_TEXT, Salary);
    share.putExtra(Intent.EXTRA_TEXT, Responsibility);
    share.putExtra(Intent.EXTRA_TEXT, Company);
    share.putExtra(Intent.EXTRA_TEXT, Contact);


    startActivity(Intent.createChooser(share, "Share via"));
}

}

Answer 1:

谁能帮助吗?

串联的六根弦成一个较大的字符串,并分享更大的字符串。



Answer 2:

您可以连接各个串较大字符串,并且可以得到它

Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
    String j = (String) b.get("name");
    JSONObject name = new JSONObject(j);

    Textv.setText(name.getString("postName"));
    TextvDesc.setText(name.getString("location"));
}


Answer 3:

有什么不对您的实际代码

让我们先来了解为什么你只得到了最后一块数据。

你的问题是在Java约定(这也适用于android.content.Intent.html#putExtra(字符串,字符串[])您使用的方法)方法“putXXX”代替实际值(如果存在)与新一你逝去。 这类似于java.util.Map.html#放(K,V)的方法。

首先可能的解决方案

对于当前的代码工作,你会需要使用不同的密钥对你的额外数据的每个时间,也就是类似的东西:

share.putExtra(SingleJobActivity.EXTRA_TEXT_NAME, PostName);
share.putExtra(SingleJobActivity.EXTRA_TEXT_LOCATION, Location);
share.putExtra(SingleJobActivity.EXTRA_TEXT_SALARY, Salary);
share.putExtra(SingleJobActivity.EXTRA_TEXT_RESPONSIBILITY, Responsibility);
share.putExtra(SingleJobActivity.EXTRA_TEXT_COMPANY, Company);
share.putExtra(SingleJobActivity.EXTRA_TEXT_CONTACT, Contact);

这将很好地工作(假设你声明为公共静态最终使用的密钥,你尊重额外的数据键,如使用全包名称为重点(如Android的合同public static final EXTRA_TEXT_NAME = "com.yourpackage.EXTRA_DATA_NAME"; )。

第二个可能的解决方案

这样做的另一种方式是通过一个额外的用字符串[](见方法的文档 )。

String[] extraParams = new String[6];
extraParams[0] = PostName;
extraParams[1] = Location;
extraParams[2] = Salary;
extraParams[3] = Responsibility;
extraParams[4] = Company;
extraParams[5] = Contact;
share.putExtra(SingleJobActivity.EXTRA_TEXT, extraParams);

然后在新的活动中,你使用这个检索阵列android.content.Intent.html#getStringArrayExtra(字符串)方法。

Intent intent = getIntent();
String[] extraParams = intent.getStringArrayExtra(SingleJobActivity.EXTRA_TEXT);


文章来源: Is it possible to share multiple text by using share intent?