Intercepting links from the browser to open my Android app

前端 未结 3 917
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 14:54

I\'d like to be able to prompt my app to open a link when user clicks on an URL of a given pattern instead of allowing the browser to open it. This could be when the user i

3条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 15:27

    Use an android.intent.action.VIEW of category android.intent.category.BROWSABLE.

    From Romain Guy's Photostream app's AndroidManifest.xml,

        
    
                        
    
            
                
                
                
                
                
            
        
    

    Once inside you're in the activity, you need to look for the action, and then do something with the URL you've been handed. The Intent.getData() method gives you a Uri.

        final Intent intent = getIntent();
        final String action = intent.getAction();
    
        if (Intent.ACTION_VIEW.equals(action)) {
            final List segments = intent.getData().getPathSegments();
            if (segments.size() > 1) {
                mUsername = segments.get(1);
            }
        }
    

    It should be noted, however, that this app is getting a little bit out of date (1.2), so you may find there are better ways of achieving this.

提交回复
热议问题