Android: Open an Activity when a custom protocol is clicked in the browser


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // This is the definition of the activity in the AndroidManifest.xml file
  2. <activity android:name=".MyActivity" android:label="@string/app_name">
  3. <!-- open the app when a foo://www.example.com link is clicked -->
  4. <intent-filter>
  5. <action android:name="android.intent.action.VIEW" />
  6. <category android:name="android.intent.category.BROWSABLE" />
  7. <category android:name="android.intent.category.DEFAULT" />
  8. <data android:scheme="foo" />
  9. </intent-filter>
  10. </activity>
  11.  
  12. // Here's how to get the data out inside the activity itself. Put in the onCreate function
  13. Intent intent = getIntent();
  14. if (Intent.ACTION_VIEW.equals(intent.getAction())) {
  15. Uri uri = intent.getData();
  16. Log.i(TAG, "Started by a user clicking " + urlStr);
  17. doSomethingWithTheUrl(uri.toString(););
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.