How can I get OS default apps in android - android

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.

Related

Programmatically getting a list of all apps in an android device

How do I get a list of all apps in an android device,which includes the system apps,and the uninstalled apps in storage.
You can easily get all Applications installed on a device using this code :
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
The pkgAppsList has all the Apps which includes system and user apps
You can try out different methods as suggested in this link
List<ApplicationInfo> infos = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
// create a list with sze of total number of apps
String[] apps = new String[infos.size()];
int i = 0;
// add all the app name in string list
for (ApplicationInfo info : infos) {
apps[i] = info.packageName;
i++;
}
Let me know if this helped you

Return of default sms app android isn't returning Messages

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);

Is it possible to open Android Wear Start Screen / Menu from an app?

I want to open Android Wear Start Screen (the one with red G icon and text 'Speak Now') from my app. Is this possible?
thanks.
w
It's not possible to launch this exact screen (no API for that).
However you can easily recreate a similar screen yourself.
This code lists the activities available in the launcher:
final PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
//using hashset so that there will be no duplicate packages,
//if no duplicate packages then there will be no duplicate apps
HashSet<String> packageNames = new HashSet<String>(0);
List<ApplicationInfo> appInfos = new ArrayList<ApplicationInfo>(0);
//getting package names and adding them to the hashset
for(ResolveInfo resolveInfo : resInfos) {
packageNames.add(resolveInfo.activityInfo.packageName);
}
//now we have unique packages in the hashset, so get their application infos
//and add them to the arraylist
for(String packageName : packageNames) {
try {
appInfos.add(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
} catch (NameNotFoundException e) {
//Do Nothing
}
}
//to sort the list of apps by their names
Collections.sort(appInfos, new ApplicationInfo.DisplayNameComparator(packageManager));
Then show the elements in appInfos into a WearableListView.
Source:
https://stackoverflow.com/a/24351610/540990

How I open gmail directly from my application?

I have this code:
Intent dialogIntent = new Intent(android.content.Intent.ACTION_SEND);
dialogIntent.setType("plain/text");
dialogIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
This opens a dialogue with DRIVE, GMAIL and SKYPE. I need to open gmail directly without the dialog appearing.
Already tried that and it does not work me. I'm doing this from a service.
add this to your intent :
dialogIntent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
Note: this is not officially supported and may break in future versions. There is no documented or official way of launching the Gmail activity.
Edit: found another method, try and and see if it works:
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);

Android: Check to see if Facebook app is present on user's device

I've kept a button which takes user to my facebook page.
In order that the official facebook app is opened I use the following url:
fb://pages/PAGE_ID
instead of http://facebook.com/PAGE_ID
Because in that case you get a list of browsers to open the url instead of teh facebook app.
It works if the user has facebook app installed. However it crashes if the user doesn't have facebook app.
Is there any way to check if the user has the facebook app?
Have you already checked this?
You can always check if an app is installed like this.
In an native android app, this is quite easy to achieve:
Uri dataUri = Uri.parse("fb://....");
Intent receiverIntent = new Intent(Intent.ACTION_VIEW, dataUri);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(receiverIntent, 0);
if (activities.size() > 0) {
startActivity(receiverIntent);
} else {
Uri webpage = Uri.parse("http://www.facebook.com/...");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
packageManager = getPackageManager();
activities = packageManager.queryIntentActivities(webIntent, 0);
if (activities.size() > 0) {
startActivity(webIntent);
}
}
I think you can reuse this code I wrote for checking if twitter app was installed on the device, to check if facebook app is installed. For twitterApps list you have to replace the values by "com.facebook.katana".
public Intent findTwitterClient() {
final String[] twitterApps = { "com.twitter.android", "com.handmark.tweetcaster", "com.seesmic", "com.thedeck.android", "com.levelup.touiteur", "com.thedeck.android.app" };
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "#hashtagTest");
tweetIntent.setType("text/plain");
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < twitterApps.length; i++) {
for (ResolveInfo resolveInfo : list) {
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(twitterApps[i])) {
tweetIntent.setPackage(p);
return tweetIntent;
}
}
}
return null;
}

Categories

Resources