简体   繁体   中英

Pattern matching for Android deep links

My app has to validate Uris like web links and deep links.

For web links like https://www.google.com I found this useful:

boolean validWebUrl =Patterns.WEB_URL.matcher(url).matches();

but it yields a negative value when used for deep links like appname://suitablestring

What is the equivalent of the above instruction for deep links?

I think that the web url matching is based on checking strings like http:// https:// and so on.

So, for my purposes, I found that this workaround could be useful:

Uri url =Uri.parse(urlString);
Intent browserIntent = new Intent();
browserIntent.setData(url);
browserIntent.setAction(ACTION_VIEW);

try {
activity.startActivity(browserIntent);
}
catch (ActivityNotFoundException e)
{

boolean validWebUrl =
        Patterns.WEB_URL.matcher(urlString).matches();
if (!validWebUrl)
    {          
      openAlertDialog(activity,activity.getResources().getString(R.string.url_not_valid_dialog_title),activity.getResources().getString(R.string.url_not_valid_error_message));
    }
else {
      openAlertDialog(activity,activity.getResources().getString(R.string.activity_not_found_for_url_dialog_title),activity.getResources().getString(R.string.activity_not_found_for_url_message));
     }
}

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