是否可以使用共享意图共享多个文本?

Is it possible to share multiple text by using share intent?

我打算分享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"));
}

}

Could anyone help ?

将六个字符串连接成一个更大的字符串,并共享那个更大的字符串。

您可以将单个字符串连接成更大的字符串,并且可以得到它

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

你的实际代码有什么问题

先了解一下为什么只得到最后一条数据

你的问题是 Java 中的约定(这也适用于你正在使用的 android.content.Intent.html#putExtra(String, String[]) 方法)方法 "putXXX" 将实际值(如果存在)替换为你正在通过的新的。 这类似于 java.util.Map.html#put(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);

这会工作正常(假设您将所使用的密钥声明为 public static final,并且您尊重 Android 额外数据密钥的合同,例如使用密钥的完整包名称(例如 public static final EXTRA_TEXT_NAME = "com.yourpackage.EXTRA_DATA_NAME";)。

第二种可能的解决方案

另一种方法是使用 String[] 传递一个额外的值(参见 method documentation)。

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

然后在新的 activity 中使用 android.content.Intent.html#getStringArrayExtra(String) 方法检索此数组。

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