Question

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

Was it helpful?

Solution

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.

OTHER TIPS

final Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top