How to listen for a custom URI

后端 未结 3 592
野的像风
野的像风 2020-11-30 09:10

I am working on an application which has its own URI prefix. (dchub:// in this case)

Searching all over and read a lot but I got a bit confused.

Is it possible

相关标签:
3条回答
  • 2020-11-30 09:49

    To register a protocol in your android app, add an extra block to the AndroidManifest.xml.

    <manifest>
     <application>
       <activity>
               <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="dchub"/>
                </intent-filter>
       </activity>
     </application>
    </manifest>
    
    0 讨论(0)
  • 2020-11-30 09:55

    Try this code:

    try {
        Uri data = getIntent().getData();
        if (data.equals(null)) { 
        } else { 
            String scheme = data.getScheme();
            String host = data.getHost();
            int port = data.getPort(); 
            //type what u want
            tv.setText("any thing");
         }      
    } catch (NullPointerException e) {
          // TODO: handle exception
      tv.setText("Null");
    }
    
    0 讨论(0)
  • 2020-11-30 09:57

    Don't use data.equals(null). That is bound to fail, you can't call methods on a null object, hence the NPE.

    Why the emtpy code block? In my mind, this is a lot prettier:

    if(data != null){
        // code here
    }
    
    0 讨论(0)
提交回复
热议问题