简体   繁体   中英

Accessing a url from a button click in android studio

So I have been trying to access a simple webpage from a button click in my app on android studio. Every method that I have tried has crashed the app when I have tried to run it. Basically I just want to click the button and then have a webpage pop up such as google. Right now I have a currently broken method that I made and am trying to use within the button click but it does not work. Any help would be appreciated.

public void openWebURL( String inURL ) {
    Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) );

    startActivity( browse );
}

public void buyButtonOnClick(View view){
    openWebURL("www.google.com");
}

You will need to launch ACTION_VIEW intent with a correctly formatted url.

public void showExternalBrowser(Activity activity, Uri uri) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(uri);
    activity.startActivity(i);
}

showExternalBrowser(activity, Uri.parse("https://www.google.com"));

Note the scheme (https) and host (www.google.com) combined make a valid url.

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