Start Application Knowing Package Name

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



Leave a reply



Submit