Match URIs with like http://example.com/something in AndroidManifest

后端 未结 3 2094
既然无缘
既然无缘 2021-02-15 03:12

I am struggling with the element in the AndroidManifest.xml file to get my URI matching working. I want to match the following URIs:

  • http
3条回答
  •  抹茶落季
    2021-02-15 03:48

    Unfortunately the wildcards that can be used for the pathPattern tag are very limited and what you want is currently impossible through pure xml.

    This is because once you accept "/.*" everything gets accepted (that include slashes). And since we can't provide data tags that are NOT to be accepted the only way is to check the data inside of your activity. Here's how to accomplish what you are up to:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Uri data = getIntent().getData();
    
        Log.d("URI", "Received data: " + data);
        String path = data.getPath();
    
        // Match only path "/*" with an optional "/" in the end.
        // * to skip forward, backward slashes and spaces
        Pattern pattern = Pattern.compile("^/[^\\\\/\\s]+/?$");
        Matcher matcher = pattern.matcher(path);
        if (!matcher.find()) {
            Log.e("URI", "Incorrect data received!");
            finish();
            return;
        }
    
        // After the check we can show the content and do normal stuff
        setContentView(R.layout.activity_main);
    
        // Do something when received path data is OK
    }
    

    Activity inside the manifest would look like this:

    
        
            
            
            
        
    
    

    If you don't want your activity to check if the data is correct you will have to change your requirements.

提交回复
热议问题