Reputation: 114
I am trying to open Chrome app from my application. Code is simple.
I got the package name of Chrome app by using this code:
PackageManager manager = Values.activity.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
for(ResolveInfo ri:availableActivities){
apps.add(ri.activityInfo.packageName);
}
//the package name of Chrome from packagemanager is "com.android.chrome"
And I tried to open Chrome like this:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.chrome");
startActivity( LaunchIntent );
But nothing happens without an error and my logcat says this:
12-28 20:28:52.298: I/ActivityManager(482): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.android.chrome cmp=com.android.chrome/com.google.android.apps.chrome.Main} from pid 20474
What am I missing? Is the package name retrieved from packagemanager wrong?
Upvotes: 2
Views: 681
Reputation: 21766
Here is generic code to launch browser from your application:
String uriString = "http://stackoverflow.com/";
Intent intent = new Intent();
intent.setData(Uri.parse(uriString));
intent.setAction(Intent.ACTION_VIEW);
if (intent.resolveActivity(getPackageManager())!= null) {
startActivity(intent);
} else {
// Any browser is not available
}
May I know why you want to launch Chrome only?
Upvotes: 2