如何通过Implicit Intents将我的数据传递给其他应用程序

时间:2017-04-21 05:00:56

标签: android android-intent implicit

我正在创建一个应用程序,其中我有一个活动,其中有一个可以输入数据的EditText。我希望通过邮件,消息传递,社交App等服务将点击按钮发送给其他人。我正在通过Implicit Intents尝试这个,但我的数据在其他应用程序中没有访问。怎么办? 我的MainAcitivity代码如下:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

  Button bt=(Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Intent i=new Intent();
                i.setAction(Intent.ACTION_SEND);
                i.setType("text/plain");
                EditText et=(EditText) findViewById(R.id.et);
                String text=et.getText().toString();
                i.putExtra("name",text);
                if (i.resolveActivity(getPackageManager()) != null) {
                    startActivity(i);
                }



                }
            }
        );
    }
}

2 个答案:

答案 0 :(得分:0)

尝试这样做:

EditText text;
Button btnSave;
String emailstring;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checking_intent);
    text = (EditText) findViewById(R.id.email);
    btnSave = (Button) findViewById(R.id.button);
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            emailstring = text.getText().toString();
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "subject of your email");
            i.putExtra(Intent.EXTRA_TEXT   , emailstring);
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(CheckingIntent.this, "There are no email app installed.", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

答案 1 :(得分:0)

试试这个

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   Button button=(Button)findViewById(R.id.button2);
  final   EditText editText=(EditText)findViewById(R.id.editText);

   button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Intent intent =new Intent(Intent.ACTION_SEND);
           intent.setType("text/plain");
           intent.putExtra(Intent.EXTRA_TEXT,editText.getText().toString());
           startActivity(intent);
       }
   });
}

}