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

时间:2015-06-22 16:33:40

标签: android button android-intent share

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

}

3 个答案:

答案 0 :(得分:0)

  

有人可以帮忙吗?

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您的实际代码有什么问题

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

您的问题按照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";)。 / p>

第二种可能的解决方案

另一种方法是使用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);

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

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