Can I use a URL in my android app?

Can I use a URL in my android app?

30.Oct.2021

 

 

Q: Can I use a URL in my android app?

 

 

 

A: Yes, but you cannot call any arbitrary URL from your application because the system will kill it instantly. The user should not notice anything about this because everything should happen automatically. You can link to other applications installed on the device or to your own application with specific data. For example, you can show an advertisement of your app in another app using special intent filters. More information here - Android Intents

Q: So how do I do that?

 

A: You need to declare some activity in order to handle links used by other apps. This is done automatically if you are building an Activity which is linked with Intent filter for ACTION_VIEW Data URI scheme (see the example). If you are not, then you can declare it yourself using intent filter (just like in ACTION_VIEW case) and get it to work.

Q: Can I use this Intent filter for my application?

 

A: Yes, but only if you want other applications to link through your app instead of directly opening an Activity. Because when the user links another application (or website), there is another activity displayed on top of yours which should really open that link. This activity could also decide to pass the information back to your app so it will be passed through your picked up Intent filter enabling your application to act on it. You need to declare an extra category which programmatically associates you with certain MIME types(for example in case of ACTION_VIEW this would probably be "video/*", "image/*", etc.).

Q: Can I do some custom stuff on my side? What if I want to open another application and not just display information received from Intent filter declaration?

A: You can do that, but you need to declare your activity as singleTop. This way it will be launched on top of everything as if the user clicked it (for example clicking a link won't bring up another activity). But note that other applications can still create instances of your activity whenever they want if they declared some intent filters which lead to your package name or your Activity class name. So make sure you don't override anything important overriding onNewIntent method.

Q: Can I use intent filters on my Activity classes without implementing an IntentReceiver?

A: Yes, you can if your application runs only as a single process (i.e. it's not started by Android OS). If it runs as a single process and you do not override onNewIntent method, the system will handle IntentReceivers for you and call your Activity class which can pick up these intents using getIntent() method and do what is needed with them.

Q: Will this work on all devices?

A: No! This works only on devices which have "activity" tag in their Application Manifest file set to true. It means that applications that don't declare any activity tags won't be launched on top of the current one. Additionally, it means that if an application is loaded as single process and a user launches a new activity from it (which overrides some Activity class), then this new activity will not run on top of that app because its "activity" tag was set to false.

Q: I have some more questions about IntentReceiver/Activity declarations...

A: Check out some more information on Android developers website - [Android] Intent Filters and [Android] Activity Declaration . If you still have questions after reading these articles, feel free to post them at stackoverflow.com with android-intent-filter tag .