【问题标题】:ACTION_SENDTO intent not working in Navigation Drawer FragmentACTION_SENDTO 意图在导航抽屉片段中不起作用
【发布时间】:2016-06-02 10:18:13
【问题描述】:

所以我正在制作这个应用程序,它包含几个使用导航抽屉链接到主要活动的片段。其中一个片段的 XML 布局包含一个按钮,单击该按钮将发送一封电子邮件。我正在模拟器上测试这个。它已在另一个应用程序上运行,但不是这个。代码如下:

public class AboutPage extends Fragment {
    public AboutPage() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_about_page, container, false);
        Button contactButton = (Button) view.findViewById(R.id.contact_button);
        contactButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("mailto:email@gmail.com"));
                intent.putExtra(intent.EXTRA_SUBJECT, "Primus Suggestion or Issue");
                intent.setType("plain/text");
                if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
                    getActivity().startActivity(Intent.createChooser(intent, "Send Email using:"));
                } else {
                        Toast.makeText(getActivity(), "You don't have any email apps to contact us.", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return view;
    }
}

它会继续显示 toast,但它适用于在 Main Activity 中创建意图的其他应用程序。

【问题讨论】:

    标签: java android android-fragments android-studio android-intent


    【解决方案1】:

    希望以下内容对您有用:-

      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"lumberjackapps@gmail.com"});
      intent.putExtra(intent.EXTRA_SUBJECT, "Primus Suggestion or Issue");
      intent.setType("text/plain");
      if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
      getActivity().startActivity(Intent.createChooser(intent, "Send Email using:"));
      }
      else {
       Toast.makeText(getActivity(), "You don't have any email apps to contact us.", Toast.LENGTH_SHORT).show();
      }
    

    【讨论】:

    • 它可以工作,谢谢你,但你知道为什么我的原始代码不起作用吗?
    • 检查ACTION_SEND 和ACTIN_SENDTO 之间的区别link "intent.resolveActivity(getActivity().getPackageManager()) != null" 对于 ACTION_SENDTO 始终为空,这就是为什么你的代码是不工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多