scheme host not working on android lollipop, click on link to open app

前端 未结 5 2003
耶瑟儿~
耶瑟儿~ 2020-12-08 16:27

I am using this piece of code to launch my app from a link.



        
5条回答
  •  無奈伤痛
    2020-12-08 17:06

    Edit:

    After testing and testing, I found that if your scheme contains an uppercase character the browser won't be able to launch it. Your scheme should only contain lowercase characters!

    Also note that bug 459156 of the chromium project still doesn't allow you to launch url's directly, you should reference users to a webpage containing this JavaScript code:

    
    
        
            
        
    
    

    Users landing on this page will automatically be prompted with either an Activity chooser dialog or directly send to your Activity.

    To try it, open the Android browser go to the url below and copy paste the above snippet in the editor: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro

    Gif showing browser + JavaScript opening the Activity

    Gif

    Original post

    I tried out your custom URI and it works on Android 5.0

    But you should be aware of the following two bugs/issues:

    1. Bug 459156 of the Chromium project This basicly means launching custom schemes from the Android browser does not work unless the URL is applied using JavaScript. For a workaround see this StackOverflow post: OAuth and custom scheme result in a "ERR_UNKNOWN_URL_SCHEME" in Chrome
    2. Bug 80971: URI with custom scheme are not clickable in SMS Text (Linkify?)

    My approach

    I created two separate Activities, one as intent receiver and the other as intent launcher. The launching activity has an EditText where the full URI can be entered and a button to launch the entered URI.

    Main.java onClick (Launching activity)

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(input.getText().toString()));
        try {
            startActivity(intent);
        } catch(ActivityNotFoundException e){
            Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
        }
    }
    

    manifest.xml (only the activities)

    Note the part. this part is important so anything after the host will be accepted as valid.

    
        
            
            
        
    
    
    
    
        
    
        
            
            
            
            
    
            
            
    
            
    
            
            
            
        
    
    

    UriActivity.java (Receiving activity)

    public class UriActivity extends ActionBarActivity {
    
        private TextView tvText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_uri);
            tvText = (TextView) findViewById(R.id.text);
            setTextFromIntent();
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setTextFromIntent();
        }
    
        private void setTextFromIntent(){
            StringBuilder text = new StringBuilder();
    
            Uri data = getIntent().getData();
            if(data != null){
                text.append("Path:\n");
                text.append(data.getPath());
    
                text.append("\n\nScheme:\n");
                text.append(data.getScheme());
    
                text.append("\n\nHost:\n");
                text.append(data.getHost());
    
                text.append("\n\nPath segments:\n");
                text.append(Arrays.toString(data.getPathSegments().toArray()));
            } else {
                text.append("Uri is null");
            }
            tvText.setText(text);
        }
    }
    

    Screenshot:

    Result of sending the custom intent to the UriActivity

    Test project download:

    I made a download for the project if you wan't to try it out yourself.

提交回复
热议问题