getting installed applications programmatically - java

I am trying to get the installed application in Android after clicking a icon. Under the icon click code I wrote following code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.ACTION_ALL_APPS);
startActivity(intent);
But it kills the application. I want to show the installed applications on the screen programmatically like when we see when press app key on Android.
Is it possible?
where am I doing wrong?

To get list of all installed application you can use the following code
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);

Related

intent.extra.NOT_UNKNOWN_SOURCE not working in Android-11

I have an installer app which installs/replaces an existing app using the below code. Up till Android-10 it works. But not in Android-11. So far my searches did not say any deprecation of android.intent.extra.NOT_UNKNOWN_SOURCE. So it should have worked.
Any idea?
Code snippet installing the app.
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
intent.putExtra("android.intent.extra.RETURN_RESULT", true);
intent.putExtra("android.intent.extra.NOT_UNKNOWN_SOURCE", true);
intent.putExtra("android.intent.extra.ALLOW_REPLACE", true);
startActivityForResult(intent, REQ_INSTALL_APK);

Android api29 How to automatically add app icon on the home screen at install?

I would like my app to automatically add the launch icon on the home screen at installation.
What I've tried already:
addShortcut method in ActivityMain
private void addShortcut() {
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher_background));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
AndroidManifest.xml
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Unfortunately, it doesn't work and I think this method is a bit out of date?
Thank You!
It is not possible to run code in direct response to your own app being installed the first time - only once the user opens your app. However, if the user installs your app from Google Play, it will generally add the icon to the home screen by default.

How can I add homescreen shortcut for app after installing

How can I add shortcut of my application to Android homescreen before I lauch app?
I need it added right after installation of app.
if you publish your app in google play store after installing app auto-create shortcut but if you want to handle that Android provides us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to the home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.
First, we need to add permission INSTALL_SHORTCUT to android manifest XML.
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
The addShortcut() method creates a new shortcut on the Home screen.
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
Note how we create shortcut Intent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT.
Finally, we broadcast the new intent. This adds a shortcut with the name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE.
Also put this code to avoid multiple shortcuts :
if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
addShortcut();
getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}

Open the default photo gallery app

I want the gallery app to be launched in a separate window, not in my app. I also dont want to choose a picture, I just want to open the default gallery app.
Some questions are very similiar to this one, but they all open the gallery not as a standalone app, always inside of the app which has called
startActivity(intent);.
see here. This is my app called SM2. inside, the default gallery app is visible, which is not the desired behaviour.
The following code has no use if there is no package named 'com.android.gallery' on the phone:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.gallery");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}
this opens a gallery in my app, not as wished as a standalone task:
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES)));
startActivity(intent1);
and this does also open the gallery in my app:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media/"));
startActivity(intent);
It is launched inside of the application
It is being launched in your task. You can use FLAG_ACTIVITY_NEW_TASK on your Intent to have it launch in its own task.
I also dont want to choose a picture, I just want to open the default gallery app
You are welcome to try using CATEGORY_APP_GALLERY, though not all gallery apps might have an activity that supports this.

using intent for launching music player

im trying to launch the music player but it just doesn't work.
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
The app crashes,it suppose to be simple but i have no idea why it doesn't work...
It might be raised ActivityNotFoundException. right?
That intent is deprecated in API Level 15 (ICS MR1).
And I guess your phone manufacturer missed that intent in music app.
Use http://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_MUSIC instead.

Categories

Resources