Its working properly in every API level, but not working properly on Lolipop(api 21)
Code is here:
Intent internetIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.google.com/"));
internetIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
internetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(internetIntent);
Check this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.google.com/"));
startActivity(intent);
Yes, it wont work in Lollipop, or may not work on any brand Manufactured(Sony,Samsung) phones, because you are trying to open an application with package name com.android.browser
internetIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
In Lollipop default browser is chrome, which has package name something else. And there is no application with package com.android.browser in Lollipop.
Same thing can happen to any other Sony/Samsung phones.
What you can do is either call for a Application Chooser Dialog or find the default browser application, and open it
1) Code for Application chooser:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
startActivity(intent);
2) Code to Find default app and launch it:
ComponentName cn=null;
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
ResolveInfo resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.activityInfo.packageName.equals("android")) {
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : resolveInfos) {
if (!info.activityInfo.packageName.equals("android")) {
cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
}
}
if(cn==null)
cn = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
} else {
cn = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
}
if(cn!=null){
Intent openIntent = new Intent();
openIntent.setComponent(cn);
openIntent.setData(Uri.parse("http://www.google.com/"));
startActivity(openIntent);
}
Related
I have a problem with sharing on Android M and exactly with creating intent chooser with filter. I've create a standard text share intent:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, getSharingText());
return intent;
Then I've applied the filter for chooser:
List<Intent> targetedShareIntents = new ArrayList<>();
List<ResolveInfo> resolves = getActivity().getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolves) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(mPromoIntent);
if (!packageName.equals("com.facebook.katana")
&& !packageName.equals("com.vkontakte.android")) {
ComponentName componentName = new ComponentName(packageName, resolveInfo.activityInfo.name);
targetedShareIntents.add(targetedShareIntent.setComponent(componentName));
}
}
if (targetedShareIntents.isEmpty()) {
return null;
}
Intent chooser = targetedShareIntents.remove(0);
return Intent.createChooser(chooser, chooserText)
.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
And the I've started the standard chooser ChooserActivity using chooser intent
startActivity(mAppsChooserIntent);
But on Android M it doesn't show chooser, it takes the first intent (in my case bluetooth) and shares with it.
I've looked through the ChooserActivity class, which in Android MNC is much bigger than in Android L and didn't find a solution where.
Does somebody know the answer or it's Android M preview bug?
I have an Android app that creates a local HTML file & then displays this to the user in a browser. I have had issues with BrowserActivity not working on different devices, subject to whatever browser is installed. My current code does the following -
public void displayStats()
{
String file = produceStats();
Uri uri = Uri.parse("file://" + file);
// chrome ??
Intent intent1 = new Intent(Intent.ACTION_VIEW);
intent1.setDataAndType(uri, "multipart/related");
// default "Internet" browser
Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
intent2.setDataAndType(uri, "text/html");
intent2.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
// any other browser (FireFox) ??
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setDataAndType(uri, "text/html");
intent3.addCategory(Intent.CATEGORY_BROWSABLE);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities1 = packageManager.queryIntentActivities(intent1, 0);
List<ResolveInfo> activities2 = packageManager.queryIntentActivities(intent2, 0);
List<ResolveInfo> activities3 = packageManager.queryIntentActivities(intent3, 0);
boolean isIntentSafe1 = activities1.size() > 0;
boolean isIntentSafe2 = activities2.size() > 0;
boolean isIntentSafe3 = activities3.size() > 0;
List<Intent> targetedShareIntents = new ArrayList<Intent>();
if (isIntentSafe1)
{
unpackResolvedIntents(uri, "multipart/related", activities1, targetedShareIntents);
}
if (isIntentSafe2) {
unpackResolvedIntents(uri, "text/html", activities2, targetedShareIntents);
}
if (isIntentSafe3) {
unpackResolvedIntents(uri, "text/html", activities3, targetedShareIntents);
}
if (targetedShareIntents.isEmpty()) {
// go to market to install app ????
Toast.makeText(plink.this, "Please install BROWSER to complete (Chrome)", Toast.LENGTH_LONG).show();
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("market://details?id=com.android.chrome"));
startActivity(goToMarket);
} else if (targetedShareIntents.size() == 1) {
startActivity(targetedShareIntents.remove(0));
} else {
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select viewer");
Intent[] extraIntents = new Intent[targetedShareIntents.size()];
for (int i = 0; i < targetedShareIntents.size(); i++) {extraIntents[i] = targetedShareIntents.get(i);}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(chooserIntent);
}
}
The produceStats() call returns the path to the file, then the rest of this function handles various different browser, and if more than one available it offers the user a Chooser.
My problem is that one user has reported the app crashing when he runs this on a Kindle HD device with the SILK browser. His stack dump is thus -
26 Jan 2014 16:26:09 GMT:Uncaught exception in java.lang.Thread:main(Unable to find explicit activity class {com.android.browser/com.android.browser.BrowserActivity}; have you declared this activity in your AndroidManifest.xml?)
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.browser/com.android.browser.BrowserActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1624)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1418)
My question is - how do I launch on a Kindle to display the file in SILK ?
Thank you
Slik on a Kindle Fire HD does not seem to support the file:// scheme for HTML, based upon an examination of its manifest. It only appears to support http://, https://, and inline://. I cannot explain the specific crash that you are encountering, as I do not see any sign of the AOSP browser app, and so I do not know why PackageManager would report otherwise.
final Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
I would like to determine if any file manager is installed or not. If not installed any, then offer user to install one.
To do what you want, you have to know Launch activity name and package name for each file manager that you want to check. After that you can do something like this:
String packageName = "com.metago.astro"
String activityName = packageName+"."+[LaunchActivityName];
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setComponent(new ComponentName(packageName, activityName));
intent.addCategory("android.intent.category.LAUNCHER");
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if(list.size() > 0){
startActivity(intent);
}else{
String googlePlayURL= "market://details?id=" + packageName;
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(googlePlayURL)));
}
How can i call an intent, to start the Downloads application, that is native in the Android OS.
I have searched a lot for this , and this is as close as I got:
Intent i = new Intent();
ComponentName comp = new ComponentName("com.sec.android.providers.downloads","com.sec.android.providers.downloads.DownloadActivity");
i.setComponent(comp);
i.setAction("android.intent.action.VIEW");
self.startActivity(i);
I have also tried:
Intent i = new Intent();
PackageManager manager = getActivity().getPackageManager();
i = manager.getLaunchIntentForPackage("com.sec.android.providers.downloads");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
but it gives me a JavaNullPointerException at: i.addCategory(Intent.CATEGORY_LAUNCHER);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri= Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
i.setData(uri);
startActivity(i);
I want to open a PPT-file with my app. How can I check if there is an app that can display these files installed on the device and how can I launch it?
Try something like this. Havent tried but might work
final Uri uri = Uri.fromFile(file);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
if(list.size() > 0)
startActivity(context, intent);
File file = new File("path_to_the_file.ppt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.ms-powerpoint");
startActivity(intent);
try with this