I'm developing a launcher app, I need to retrieve an Android OS default Phone app, Browser app and SMS apps', application Info (Application name, Package name, Launcher icon). Following code is used to get all launchable applications.
private static List<ApplicationInfo> getInstalledApps(Context context, PackageManager pm) {
List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(0);
List<ApplicationInfo> laughableInstalledApps = new ArrayList<>();
for(int i =0; i<installedApps.size(); i++){
if(pm.getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
laughableInstalledApps.add(installedApps.get(i));
}
}
return laughableInstalledApps;
}
After spending some time with the code, I found a way get what I wanted.
Default Dial App
Intent mainIntent = new Intent(Intent.ACTION_DIAL, null);
mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
ActivityInfo info = pkgAppsList.get(0).activityInfo;
Default SMS App
String smsPkgName = Telephony.Sms.getDefaultSmsPackage(context);
ApplicationInfo info = getPackageManager().getApplicationInfo(smsPkgName, 0);
Default Browser App
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent,
PackageManager.MATCH_DEFAULT_ONLY);
ActivityInfo info = resolveInfo.activityInfo;
Try with PackageManager#getPreferredActivities(java.util.List, java.util.List, java.lang.String), where the first parameter is a list of IntentFilter, which you would like to get default apps for. Then the answer is written in list passed as second parameter.
Here are some common intents for which you might try to find default apps.
I create button that use "mailto" to send email via email applications,when i click on the button it shows 2 option(gmail,outlook). Is there a possible way to show only the outlook application?
Yes, there is a possible way. You need to get package manager first. And iterate over the list of available matches that is received via queryIntentActivities() method. And find the app by passing package name of that outlook app. Code is below.
Intent outlookIntent = new Intent(Intent.ACTION_SEND);
outlookIntent.setType("text/html");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(outlookIntent, 0);
String outlookActivityClass = null;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.equals("com.microsoft.office.outlook")) {
outlookActivityClass = info.activityInfo.name;
if (outlookActivityClass != null && !outlookActivityClass.isEmpty()) {
break;
}
}
}
outlookIntent.setClassName("com.microsoft.office.outlook", outlookActivityClass);
outlookIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail#gmail.com" });
outlookIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
outlookIntent.putExtra(Intent.EXTRA_CC, "cc#gmail.com"); // if necessary
outlookIntent.putExtra(Intent.EXTRA_TEXT, "Email message");
outlookIntent.setData(Uri.parse("yourmail#gmail.com"));
startActivity(outlookIntent);
Hope this will help you out.
I am working with this code which Ragu Swaminathan helped me with on my original post found at: How to show both texting and dialer apps with a single Intent on Android?.
final List<Intent> finalIntents = new ArrayList<Intent>();
final Intent textIntent = new Intent(Intent.ACTION_VIEW);
textIntent.setType("text/plain");
textIntent.setData(Uri.parse("sms:"));
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(textIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(textIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
finalIntents.add(intent);
}
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:121"));
Intent chooserIntent = Intent.createChooser(
callIntent, "Select app to share");
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, finalIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
I have this code that brings up snackup separating sms and dialer apps installed on users phone and allows them to pick one to either send a message / call a person.
What I want to know is, is it possbile to add another section on this snackbar that checks if the user has whatsapp installed or another specfic app installed.
Also, being new to android, I have tried playing around with the code but ended up messing it up. another thing that I would like to do is create sections like theses;
Sms apps:
.....
.....
.....
Dialer apps:
.....
.....
.....
Whatsapp:
.....
Any help is welcomed, please let me know if my question is not clear.
Edited;
You can use getPackageInfo method
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageInfo(certainAppPackageName, 0);
if (pi != null) {
//app is installed, do smth
}
Google play links exist package names.
For example:
https://play.google.com/store/apps/details?id=org.telegram.messenger
where org.telegram.messenger is a package name
Here's my code:
Intent i = (new Intent(Intent.ACTION_SEND, Uri.fromParts("sms", "12345678", null)));
PackageManager pm = getApplicationContext().getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY);
Log.i(TAG, "apk:"+mInfo.activityInfo.packageName);
I get back:
apk:com.mysms.android.sms
No matter what the default app is.
If I go into settings and switch the default app to messages, I never get back com.android.sms (or whatever messages package is). I specifically need the default message app in the form of com.whatever.its.name.
UPDATE: I gave up too soon! In case anyone else is interested in this, this worked for me:
String defApp = Settings.Secure.getString(getContentResolver(), "sms_default_application");
PackageManager pm = getApplicationContext().getPackageManager();
Intent iIntent = pm.getLaunchIntentForPackage(defApp);
ResolveInfo mInfo = pm.resolveActivity(iIntent,0);
Log.i(TAG, "apk:"+mInfo.activityInfo.packageName);
I am trying to open up Gmail Compose screen when a button is clicked in my Android App.
Do I need some API key for this from Google? or what do I need to do in my button onClickListener?
Any kind of insight is much appreciated.
As JeffC pointed out, it is easy to essentially tell Android that you want to send something email-like and have Android give users a list of choices, which will probably include GMail. If you specifically want GMail, you have to be a bit cleverer. (Note that the correct MIME type is actually "text/plain", not "plain/text". Do to an implementation oddity, GMail seems to be the only activity which responds to the latter, but this isn't a behavior I would count on.)
The following App demonstrates the principle you can follow: actually examine all of the activities which say they can handle your SEND intent and see if any of them look like GMail.
package com.stackoverflow.beekeeper;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import java.util.List;
public class StackOverflowTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") ||
info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
startActivity(intent);
}
}
try {
Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_email"));
intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
intent.putExtra(Intent.EXTRA_TEXT, "your_text");
startActivity(intent);
} catch (ActivityNotFoundException e){
//TODO smth
}
I don't know that you can specifically launch gmail. Have you tried this in your onClickListener
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
startActivity(emailIntent);
You can find more details here: Email android intent
You just place below code inside your click event. Will open directly gmail as compose mode, Output screenshot attached below.
Happy coding :-)
code :
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"mailto#gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,"Subject text here...");
intent.putExtra(Intent.EXTRA_TEXT,"Body of the content here...");
intent.putExtra(Intent.EXTRA_CC,"mailcc#gmail.com");
intent.setType("text/html");
intent.setPackage("com.google.android.gm");
startActivity(Intent.createChooser(intent, "Send mail"));
Output :
Just Place the set of code in your click event/trigger event and it will directly navigate you to the native gmail application with all the details pre-filled.
All the email attributes/details are there in the set of code below(Comments added).
Intent intent = new Intent(Intent.ACTION_SEND);
String[] recipients = {"recipient#gmail.com"};//Add multiple recipients here
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject"); //Add Mail Subject
intent.putExtra(Intent.EXTRA_TEXT, "Enter your mail body here...");//Add mail body
intent.putExtra(Intent.EXTRA_CC, "mailcc#gmail.com");//Add CC emailid's if any
intent.putExtra(Intent.EXTRA_BCC, "mailbcc#gmail.com");//Add BCC email id if any
intent.setType("text/html");
intent.setPackage("com.google.android.gm");//Added Gmail Package to forcefully open Gmail App
startActivity(Intent.createChooser(intent, "Send mail"));
#HAPPY_CODING
public static void openGmail(Activity activity,String[] email, String subject, String content) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
final PackageManager pm = activity.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
ResolveInfo best = null;
for(final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
best = info;
if (best != null)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
activity.startActivity(emailIntent);
}
<TextView
android:id="#+id/EmailId"
android:linksClickable="true"
android:autoLink="email"
android:text="info#stackoverflow.com"
/>
This is the best method to send email on click of textView.
This code will directly start the gmail application to send an email.
I found out using this post that the important part here is to find the "packageName" and the "activityInfo.name"
I wanted to only use gmail without a chooser. Note that the package name is hard coded so if Gmail changes its packagename it won't work any more.
The key to this was the setComponent where the first param is the package name and the second param is the activityInfo name.
But like i said use with caution, I repeat myself; if the user doesn't have the gmail app installed or gmail changes its package name or Activty name to send an email this (hard)code will break. Thy have been warned ;)
Here is my code
Intent myIntent = new Intent(Intent.ACTION_SEND);
PackageManager pm = getPackageManager();
Intent tempIntent = new Intent(Intent.ACTION_SEND);
tempIntent.setType("*/*");
List<ResolveInfo> resInfo = pm.queryIntentActivities(tempIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
if (ri.activityInfo.packageName.contains("android.gm")) {
myIntent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"exampleto#gmail.com"});
myIntent.setType("message/rfc822");
myIntent.putExtra(Intent.EXTRA_TEXT, "extra text");
myIntent.putExtra(Intent.EXTRA_SUBJECT, "Extra subject");
myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("uri://your/uri/string");
}
}
startActivity(myIntent);
You can use Simple Intent.ACTION_SEND intent
set Intent.EXTRA_EMAIL for array of emails
set Intent.EXTRA_SUBJECT for subject line in email composer
Explore more EXTRA options available here -> https://developer.android.com/guide/components/intents-common#Email
Here's a quick code snippet
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
if (intent.resolveActivity(ctx.getPackageManager()) != null) {
startActivity(intent);
}
if you don't get anything in this line
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
then replace this line with
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 1);
Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain")
.putExtra(Intent.EXTRA_EMAIL, new String[]{emails});
List<ResolveInfo> matches = activity.getPackageManager().queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
best = info;
}
}
if (best != null) {
intent.setClassName(best.activityInfo.packageName,best.activityInfo.name);
}
activity.startActivity(intent);