How to create a chooser for a list of applications using package manager

百般思念 提交于 2019-12-24 16:10:41

问题


Basically I have a list of package names for popular email apps, and I want to create a chooser to launch the send email intent, you can refer to this question, here he uses the package manager for just gmail, I want to do it for a list of packages


回答1:


What I want to do is simulate the desktop behavior when you click on an email link which opens outlook/gmail client with the to field set to the email id, but in addition to this I want to let the user choose the email application that is launched

startActivity(new Intent(Intent.ACTION_SENDTO)
  .setData(Uri.parse("mailto:"+yourEmailAddressGoesHere)));

where you replace yourEmailAddressGoesHere with "the email id".

If the user has more than one email client, and the user has not chosen a default email client, the user will get a chooser automatically. If the user has only one email client, or has chosen a default email client, this will lead the user to some activity to compose a message to your designated email address.




回答2:


Create the intent add set its uri data

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);

then create the chooser intent. It's useful when you want the user to choose the app he wants every time he sends an email. If you want to choose an app and make it a default, you can omit the chooser and start the intent directly

Intent chooser = Intent.createChooser(intent, "Chooser title");

then check if there is at least one activity that can handle the email intent

if(intent.resolveActivity(getPackageManager()) != null){
    // there are apps, start the chooser
    startActivity(chooser);
} else {
    // no apps found
    Toast.makeText(this, "No apps found", Toast.LENGTH_SHORT).show();
}


来源:https://stackoverflow.com/questions/34635746/how-to-create-a-chooser-for-a-list-of-applications-using-package-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!