Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - is it possible to use private intents instead of global ones?

background: i've noticed that for regular activities within, it is possible for any application to open the activities of my app .

question: is it possible to allow only my own app (or apps , or package) to send and receive intents inside the same scope , so that other application won't be able to receive them or interfere with the flow of the app?

example: suppose i have a broadcastReceiver that listens to some kind of intent , but this intent is only meant to be used by another service/activity that resides either inside my app , or inside another app that i've created , but i don't want others to be able to use this intent.

please help me.

like image 767
android developer Avatar asked Mar 16 '12 13:03

android developer


2 Answers

setPackage()

Set an explicit application package name that limits the components this Intent will resolve to. If left to the default value of null, all components in all applications will considered. If non-null, the Intent can only match the components in the given application package.

or you can use setSelector() , but not both.

like image 150
Reno Avatar answered Sep 21 '22 13:09

Reno


suppose i have a broadcastReceiver that listens to some kind of intent , but this intent is only meant to be used by another service/activity that resides either inside my app , or inside another app that i've created , but i don't want others to be able to use this intent.

In addition to Reno's fine answer, for your specific requirement quoted above, use LocalBroadcastManager. Not only do you get the security you seek, but it is more efficient. LocalBroadcastManager is available in the Android Support package and AFAIK should work going back to Android 1.6. Here is a sample project using LocalBroadcastManager.

like image 32
CommonsWare Avatar answered Sep 25 '22 13:09

CommonsWare