android - 在 ListView 适配器中从我的应用程序打开电子邮件应用程序

标签 android listview android-intent

<分区>

我试图从我的应用程序打开电子邮件应用程序,但出现错误。

Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag.

问题是,我设置了 FLAG_ACTIVITY_NEW_TASK。 我也尝试过 addFlags

这是我的代码:

 private void mailTo(String mail) {
    Intent i = new Intent(Intent.ACTION_SEND).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{mail});
    try {
            context.startActivity(Intent.createChooser(i, ""));
} catch (ActivityNotFoundException ex) {
        Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
    }
}

我必须指出,在同一个类中,我有更多有效的 Intent Actions。

private void callTo(String number) {
    Intent callIntent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.setData(Uri.parse("tel:" + number));
    context.startActivity(callIntent);
}

和:

private void smsTo(String phoneNumber) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

最佳答案

您是否尝试调用 startActivity() 而不是从 context() 对象获取它? context() 具有应用程序的当前状态,因此您可能不确定从哪里开始 Intent 。所以它会是:

startActivity(Intent.createChooser(i, ""));

然后,删除标志

Intent i = new Intent(Intent.ACTION_SEND);

编辑

由于您是从不是 MainActivity()Activity() 调用 intent(),请尝试传递 context() 参数到您的类的构造函数。

Context context;

public MyAdapaterClass(Context context) {
this.context=context;
}

然后像这样启动intent():

Intent i = new Intent(context, MainActivity.class);

关于android - 在 ListView 适配器中从我的应用程序打开电子邮件应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39334361/

相关文章:

listview - QML,动态添加元素到 ListView

android - 如何从 Android 模拟器替换/卸载电子邮件应用程序

android - Android系统杀死进程: does the on going notifications are removed too?

c# - DataTemplate 中带有 DoubleAnimation 的 Storyboard

java - 带有 Intent 的空对象引用

java - 查询 Android 浏览器历史记录中的 URL

android - 在屏幕android上更改对话框的位置

android - 在整个应用程序中将 android 方向锁定为横向?

android - Android Studio 中的 color.xml 文件出现奇怪的错误

.net - 检查 CheckedListBox 中的项目而不选择