当我使用i.setType("text/plain");
时,我的按钮正在工作,但是当没有使用时则它无法正常工作。点击它后没有提供打开任何应用程序的选项。为什么这样?。请帮帮我。
以下是MainActivity的代码:
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(Intent.EXTRA_SUBJECT,"Email From Ankit's Own
Created App");
i.putExtra(Intent.EXTRA_TEXT,text);
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}
}
});
}
}
答案 0 :(得分:0)
您应该按照以下方式使用startActivity:
try {
startActivity(Intent.createChooser(emailIntent,
"Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(),
"No email clients installed.",
Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
电子邮件应用需要获取您的数据
获取共享文件的更详细示例,您可以查看here
答案 2 :(得分:0)
试试这个,
/* Create the Intent */
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to@email.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
/* Send it off to the Activity-Chooser */
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));