Verify that Intent can be launched


// TODO (1) Create a method called openWebPage that accepts a String as a parameter

// Do steps 2 - 4 within openWebPage

    // TODO (2) Use Uri.parse to parse the String into a Uri

    // TODO (3) Create an Intent with Intent.ACTION_VIEW and the webpage Uri as parameters

    // TODO (4) Verify that this Intent can be launched and then call startActivity

public void openWebPage(String url){
    Uri uri = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
    if(intent.resolveActivity(getPackageManager()) != null){
        startActivity(intent);
    }
}

When you call startActivity() or startActivityForResult() and pass it an implicit intent, the system resolves the intent to an app that can handle the intent and starts its corresponding Activity. If there's more than one app that can handle the intent, the system presents the user with a dialog to pick which app to use.
This page describes several implicit intents that you can use to perform common actions, organized by the type of app that handles the intent. Each section also shows how you can create an intent filter to advertise your app's ability to perform the same action.

Caution: If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.

Bu blogdaki popüler yayınlar

About Android padding, margin, width, height, wrap_content, match_parent, R Class

@SerializedName and @Expose annotations