Android BrowserActivity on Kindle HD - android

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);

Related

How to redirect to youtube app through link from Android app?

In my UI, i have a textview to shows list content. I have auto link and app chooser dialog enabled for that textview. In case of youtube link as content (like https://youtu.be/x), upon selecting youtube app from the app chooser dialog, youtube app opens with an error screen (error code 400).
I am not sure why its happening. I have provided my code below.
public static void getAppChooserDialog(Context context, String url){
List<Intent> targetedShareIntents = new ArrayList<>();
List<String> sharePackages = new ArrayList<>();
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(createViewIntent(url),0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = createViewIntent(url);
if (!info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName()) &&
!sharePackages.contains(info.activityInfo.packageName)) {
if (targetedShare != null) {
targetedShare.setPackage(info.activityInfo.packageName);
sharePackages.add(info.activityInfo.packageName);
}
targetedShareIntents.add(targetedShare);
}
}
if(!targetedShareIntents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
context.getString(R.string.complete_action_using));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[]{}));
context.startActivity(chooserIntent);
}
}
private Intent createViewIntent(String url){
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.setData(Uri.parse(url));
return shareIntent;
}
The YouTube app doesn't support intent with the normal url (no idea why).
For an url https://youtu.be/videoId you need to share vnd:youtube:videoId

Android M sharing chooser filter

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?

Open PDF-File in Android not working

I try to open a pdf file from my apps directory through a pdf viewer on the device.
PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p;
try {
p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (NameNotFoundException e) {
Log.w("Error", "Error Package not found ", e);
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s + "\\Document.pdf"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
Intent crC = Intent.createChooser(intent, "Open File"); startActivity(crC);
On the test device is an pdf viewer installed. Nevertheless I'm told that no existing app is able to open that file. Am I doing something wrong?
If anybody is still interested, here is my solution:
I simply changed the code from this:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s + "\\Document.pdf"));
intent.setType("application/pdf");
to this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(s + "\\Document.pdf"), "application/pdf");
And suddenly it worked. :)
What is the following code supposed to do?
PackageManager pm = getPackageManager();
Intent crC = Intent.createChooser(intent, "Open File"); startActivity(crC);
I assume you looked at a few tutorials and got a mixture of everything in here. Instead of those two lines, just try
startActivity(intent);
Does that not work?

Limiting Android PackageManager to a single choice

My application evokes the android PackageManager when a file is chosen and the user is presented with a choice of applications to handle how the file should be dealt with. I want to limit this choice to Bluetooth. Currently Bluetooth comes up as the first option which is fine and this all works. I was wondering if its possible to only present the user with this single option.
case REQUEST_FILE_SELECT:
if (requestCode == REQUEST_FILE_SELECT) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d(TAG, "File Uri: " + uri.toString());
// Get the path
String path = null;
try {
path = FileUtils.getPath(this, uri);
} catch (URISyntaxException e) {
e.printStackTrace();
}
Log.d(TAG, "File Path: " + path);
// Get the file instance
File mFile = new File(path);
// Evoke the file chooser
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("*/*");
// Evoke the package manager
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(shareIntent,
PackageManager.GET_ACTIVITIES);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
if (packageName.equals("com.android.bluetooth")) {
Intent targetedShareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(mFile));
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
startActivity(Intent.createChooser(shareIntent,
"Share File"));
}
}
}
}
Solution: find out which apps does the device support for your intent, find the one that is bluetooh, invoke it directly.
This article answers your question:
http://tsicilian.wordpress.com/2012/11/06/bluetooth-data-transfer-with-android/
From the article:
We can see that the BT application is among those handlers. We could of course let the user pick that application from the list and be done with it. But if we feel we should be a tad more user-friendly, we need to go further and start the application ourselves, instead of simply displaying it in a midst of other unnecessary options…But how?
One way to do that would be to use Android’s PackageManager this way:
//list of apps that can handle our intent
PackageManager pm = getPackageManager();
List appsList = pm.queryIntentActivities( intent, 0);
if(appsList.size() > 0 {
// proceed
}
The above PackageManager method returns the list we saw earlier of all activities susceptible to handle our file transfer intent, in the form of a list of ResolveInfo objects that encapsulate information we need:
//select bluetooth
String packageName = null;
String className = null;
boolean found = false;
for(ResolveInfo info: appsList){
packageName = info.activityInfo.packageName;
if( packageName.equals("com.android.bluetooth")){
className = info.activityInfo.name;
found = true;
break;// found
}
}
if(! found){
Toast.makeText(this, R.string.blu_notfound_inlist,
Toast.LENGTH_SHORT).show();
// exit
}
We now have the necessary information to start BT ourselves:
//set our intent to launch Bluetooth
intent.setClassName(packageName, className);
startActivity(intent);
What we did was to use the package and its corresponding class retrieved earlier. Since we are a curious bunch, we may wonder what the class name for the “com.android.bluetooth” package is. This is what we would get if we were to print it out: com.broadcom.bt.app.opp.OppLauncherActivity. OPP stands for Object Push Profile, and is the Android component allowing to wirelessly share files.
Also in the article, how to enable the bluetooth from your application.

Open a facebook page from android app?

How can I start an intent to open a Facebook application on a phone and navigate to the prefered page in Facebook?
I tried:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
intent.putExtra("extra_user_id", "123456789l");
this.startActivity(intent);
Well, whatever I write to "1234567891", it is always navigating to my page. Always to me and not else.
How could I do this?
Here is the best and simple way to do it.
Just follow the code
public final void Facebook() {
final String urlFb = "fb://page/"+yourpageid;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlFb));
// If Facebook application is installed, use that else launch a browser
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() == 0) {
final String urlBrowser = "https://www.facebook.com/pages/"+pageid;
intent.setData(Uri.parse(urlBrowser));
}
startActivity(intent);
}
I had the exactly same problem, sent the user id but for some reason, my profile always opened instead of the friend's profile.
The problem is that if you pass the String of the Long object that represents the Facebook UID, or even a long primitive type, the intent won't be able to read it later. You need to pass a real Long.
So the complete code is:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
Long uid = new Long("123456789");
intent.putExtra("extra_user_id", uid);
startActivity(intent);
Ok enjoy and hope this helps :-)
Maxim
try this code :
String facebookUrl = "https://www.facebook.com/<id_here>";
try {
int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) {
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
Uri uri = Uri.parse("fb://page/<id_here>");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
}
This solution won't work any more. The new version of the facebook app doesn't support anymore those kind of intents. See here the bug report
The new solution is to use the iPhone scheme mechanism (Yes, facebook decided to support the iPhone mechanism in Android instead of the implicit intent mechanism of Android).
So in order to open the facebook app with a user profile all you need to do is:
String facebookScheme = "fb://profile/" + facebookId;
Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
startActivity(facebookIntent);
If you are looking for other actions you can use the following page for all available actions (/you have to test it though, since I didn't find an official publication of facebook about this)

Categories

Resources