Android 和 Phonegap : Intent to launch an other Application

标签 android cordova android-intent phonegap-plugins

我发现这段代码可以启动浏览器,目的是:

Intent intent = new Intent (Intent.ACTION_VIEW, uri);
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
intent.setDataAndType(uri, "text/html");
startActivity(intent);

有效。

但我没有使用 Android SDK。我正在使用 Phonegap 来制作我的应用程序。 所以我正在寻找一种方法来启动这个 Intent 。 我找到了这个插件: https://github.com/borismus/phonegap-plugins/tree/master/Android/WebIntent/

但它不允许“setClassName”、“setDataAndType”......

有什么想法吗?

最佳答案

如果你勾选CordovaWebViewClient Cordova 开箱即用地处理了几个 uri。

如果您不想使用插件,您可以覆盖它并添加对不同的自定义类型的 uris 的支持,这反过来会使用 uri 中指定的参数启动您选择的应用程序。

public class CustomWebViewClient extends
        org.apache.cordova.CordovaWebViewClient {

    public CustomWebViewClient(DroidGap ctx) {
        super(ctx);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        //Check if provided uri is interesting for us
        if (url.startsWith("customUri://")) {
            //Get uri params, create intent and start the activity
            return true;
        }

        return super.shouldOverrideUrlLoading(view, url);
    }
}

上面的类应该在你的 android 项目的主要 Activity (扩展 DroidGap)中使用:

@Override
public void init() {
    WebView webView = new WebView(this);
    CustomWebViewClient webClient = new CustomWebViewClient(this);
    CordovaChromeClient chromeClient = new CordovaChromeClient(this);
    super.init(webView, webClient, chromeClient);
}

您提到的插件允许根据指定的 uri 启动 Intent 。因此,例如,如果您提供以 http://开头的 uri,android 会将其识别为网络资源并尝试启动可用的浏览器。因此,如果您只想启动浏览器,这对您来说应该足够了。

关于Android 和 Phonegap : Intent to launch an other Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685220/

相关文章:

android - cordova-icon 实用程序抛出错误 "spawn convert ENOENT"

面向 Cordova 的应用程序 POST cookie session 信息或传递给默认浏览器

java - Android:在使用短信 Intent 后返回 Activity

java - 如果 Android Activity 位于前台,则向其传递通知 Intent 并启动它的适当方法是什么?

android - 如何解析从 EditText 到 TextView 的 double ? (安卓)

android - 从位图创建可绘制对象

java - @context httpServletRequest 对 jersey 中 post 方法的请求为空

java - 当从 Android 的锁定屏幕上单击通知操作时,如何以编程方式解锁屏幕?

javascript - 如何在android PhoneGap中加载动态div的iscroll

java - Android 应用程序无法在设备上启动