Android and Phonegap: Intent to launch an other Ap

2019-06-06 05:17发布

问题:

I found this code to launch the browser with an intent:

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

It works.

But i'm not using Android SDK. I'm using Phonegap to make my application. So i'm looking for a way to launch this intent. I found this plugin: https://github.com/borismus/phonegap-plugins/tree/master/Android/WebIntent/

But it does not allow to "setClassName", "setDataAndType"....

Any ideas ?

回答1:

If you just want to load a url in the Android browser then you should just use the ChildBrowser plugin:

https://github.com/phonegap/phonegap-plugins/tree/master/Android/ChildBrowser

If you are looking to start any intent then just let me know as the Android PhoneGap code launches all sorts of intents to get work done.



回答2:

If you check CordovaWebViewClient there are several uris which are handled by Cordova out of the box.

If you don't want to play with plugins you could override it and add support for different, custom types of uris which in turn would lunch the application of your choice with parameters specified in the 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);
    }
}

Above class should be used in the main activity (which extends DroidGap) of your android project:

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

The plugin which you mentioned allows to lunch an intent based on the uri specified. So for example if you provide uri starting with http:// android will recognize it as web resource and will try to launch available browser. So if you just want to launch browser it should be enough for you.