简体   繁体   中英

Intent to open a link within a browser not working

I'm working on an Android Coursera assignment to open a link within a browser using Implicit Intents. Within the code, I have a chooser method that gives one the option of which browser they would like to use. The problem though is that no browser appears using the CATEGORY Browsable option. Please look at the my code:

private void startImplicitActivation() {

        Log.i(TAG, "Entered startImplicitActivation()");

        // TODO - Create a base intent for viewing a URL
        // (HINT:  second parameter uses Uri.parse())


        String url = "https://www.google.com";
        Intent baseIntent = new Intent(Intent.CATEGORY_BROWSABLE, Uri.parse(url));


        // TODO - Create a chooser intent, for choosing which Activity
        // will carry out the baseIntent
        // (HINT: Use the Intent class' createChooser() method)
        Intent chooserIntent = new Intent(Intent.createChooser(baseIntent, "Select Your Browser"));


        Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());


        // TODO - Start the chooser Activity, using the chooser intent
        startActivity(chooserIntent);


 }

The resulting output when launched in an activity is below (I don't understand why no browsers come up):

用故意调用的createChooser()方法

You can use this:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("your url")); 
startActivity(intent);

I think the code directly opens the popup that which browsers is installed in your phone. You don't write specific code for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM