start application knowing package name
Try using PackageManager
and getLaunchIntentForPackage()
Launch app using package name
Try this :
String pack[] = {"com.android.setting", "com.android.browser"};
try {
Intent intent = getPackageManager().getLaunchIntentForPackage(pack[0]);
startActivity(intent);
} catch (Exception e) {
// returns null if application is not installed
}
How to open application android programmatically if i don't know package name
Maybe something like this would help, but I have to add that this is a terrible approach as app names can be the same for different apps, and some apps (like "Contacts") have different labels based on the language set
/**
*
* @param appName The target app to launch, if there are multiple apps with same name, the first found will be launched
* @throws IllegalArgumentException If the target app was not found
*/
public void runApp(String appName) throws IllegalArgumentException {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
for ( ResolveInfo info : getPackageManager().queryIntentActivities( mainIntent, 0) ) {
if ( info.loadLabel(getPackageManager()).equals(appName) ) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(info.activityInfo.applicationInfo.packageName);
startActivity(launchIntent);
return;
}
}
throw new IllegalArgumentException("Application not found!");
}
Open Application in Android by label name, and unknowing package name.
thanks for all replies, i solve it by @CommonsWare solution and it works correctly :
class Applications {
private String packageName;
private String labelName;
}
private void getAllApps() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(mainIntent, 0);
for (ResolveInfo resolveInfo : activities) {
Applications applications = new Applications();
applications.labelName = resolveInfo.loadLabel(getPackageManager())
.toString().toLowerCase();
applications.packageName = resolveInfo.activityInfo.packageName
.toString();
applicationsArrayList.add(applications);
}
}
private void openApplication(String appName) {
String packageName = null;
// matching the package name with label name
for (int i = 0; i < applicationsArrayList.size(); i++) {
if (applicationsArrayList.get(i).labelName.trim().equals(
appName.trim())) {
packageName = applicationsArrayList.get(i).packageName;
break;
}
}
// to launch the application
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage(packageName);
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
}
Related Topics
Lazy Download Images into Gridview
Only Load Layout When Firebase Calls Are Complete
Failed Binder Transaction When Putting an Bitmap Dynamically in a Widget
Dynamic Listview Adding "Load More Items" at the End of Scroll
Android - How to Make Slide Menu Like Facebook, Spotify and Google +
Android Broadcast Receiver Bluetooth Events Catching
Resizing Layouts Programmatically (As Animation)
Ontaskremoved() Not Getting Called in Huawei and Xiaomi Devices
How to Put Media Controller Button on Notification Bar
Location Needs to Be Enabled for Bluetooth Low Energy Scanning on Android 6.0
Device Not Detected in Eclipse When Connected with Usb Cable
Change the Text Color of Numberpicker
Android: Programmatically Detect If Device Has Hardware Menu Button
Android Take Screen Shot Programmatically
How to Solve the Install_Failed_Dexopt Error
Webview Link Click Open Default Browser
Flutter Run Error:You Have Not Accepted the License Agreements