Kindle HD上的Android BrowserActivity

时间:2014-01-28 19:02:31

标签: android browser kindle

我有一个Android应用,可以创建一个本地HTML文件&然后在浏览器中将其显示给用户。我遇到了BrowserActivity无法在不同设备上运行的问题,无论安装什么浏览器。我目前的代码执行以下操作 -

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

}

produceStats()调用返回文件的路径,然后该函数的其余部分处理各种不同的浏览器,如果有多个可用浏览器,它会为用户提供一个选择器。

我的问题是,一个用户在使用SILK浏览器在Kindle HD设备上运行此应用程序时报告该应用程序崩溃。因此他的堆栈转储 -

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)

我的问题是 - 如何在Kindle上启动以SILK显示文件? 谢谢

2 个答案:

答案 0 :(得分:2)

基于对其清单的检查,在Kindle Fire HD上的Slik似乎不支持HTML的file://方案。它似乎只支持http://https://inline://。我无法解释您遇到的具体崩溃,因为我没有看到任何AOSP浏览器应用程序的迹象,因此我不知道为什么PackageManager会报告其他情况。

答案 1 :(得分:1)

final Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);