问题
My app should be able to send the text in a TextView via WhatsApp, Email, SMS etc. For that i need a list of installed Messging Applications. I tried it with the PackageManger but i get all apps. How can i get only the Instant Messaging Apps?
This is my code to list the installed apps:
PackageManager packageManager=this.getPackageManager();
List<PackageInfo> applist=packageManager.getInstalledPackages(0);
Iterator<PackageInfo> it=applist.iterator();
while(it.hasNext()){
PackageInfo pk=(PackageInfo)it.next();
if(PackageManager.PERMISSION_GRANTED==(packageManager.checkPermission(Manifest.permission.INTERNET, pk.packageName)& packageManager.checkPermission(Manifest.permission.RECEIVE_SMS, pk.packageName))) //checking if the package is having INTERNET permission
{
myList.add(""+pk.applicationInfo.loadLabel(packageManager));
}
}
回答1:
Supposed you manage to get the list of the apps you want, then what are you going to do with them? I think that you need to let android to present a list of apps to your users for them to choose which application they want to handle the text, depending on the action performed. Fortunately this is a build in feature in Android. Here is my function for sending e-mails:
public static void StartEmailIntent (Context cx, String EmailAddress){
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("plain/text");
email.putExtra(Intent.EXTRA_EMAIL, new String[]{EmailAddress});
cx.startActivity(Intent.createChooser(email, cx.getString(R.string.dlg_sendmail_selectortitle)));
}
As you can see I am setting Intent.ACTION_SEND as the action and then with the Intent.createChooser android creates a list of applications capable to handle that action based on the type and the extras of the Intent. It shouldn't be hard to adapt other actions like SMS, Phone calls etc. You can read more about it here Sending Content to Other Apps
Hope this helps...
回答2:
If you are targeting Ice Cream Sandwich you should go with the ShareActionProvider. There you get your desired list of ways to share whatever you want.
You could also read this android-developer-blogpost where they explain how to share via intent. So for example for your emailsharing:
Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide what to do with it.
intent.putExtra(Intent.EXTRA_SUBJECT, “Some Subject Line”);
intent.putExtra(Intent.EXTRA_TEXT, “Body of the message, woot!”);
来源:https://stackoverflow.com/questions/10504459/how-to-get-list-of-installed-instant-messenger-apps