5

Can an app run code if you don't open it?

As part of this question, the topic of an app downloading data without ever being explicitly opened by the user was mentioned briefly.

It got me thinking: is it trivial for an app to run code without ever being opened by the user?

Obviously, an app could just be set to launch on boot, causing it to run on the next power cycle. But even easier, couldn't an app just have a receiver for a very common function (such as turning the screen on), thus running itself without the user being aware?

Is it even possible for an app to act as a receiver to an alarm timer so that it automatically gets run every minute?

Many people run App Ops right after installing an app to limit exposure. But it seems like it would be almost trivial for an app to run code immediately after being installed.

Is this correct, or are there safeguards to prevent this?

  • Yes, services run automatically. – Matthew Read May 2 '15 at 15:20
  • There are a lot of ways to answer your question(s), but there are variables. Being rooted and SU is installed, what system and user components did you GRANT full access to? Do you download and install apps from reliable sources? Do you read the permissions being allowed to an app? Do you use Ease of Accessibility apps?.......as far as AppOps, I don't use that. My tool of choice is My Android Tools. You've got options to turn on/off services, activities, broadcast receiver, content provider and other items. – HasH_BrowN May 4 '15 at 0:08
  • @HasH_BrowN I'm thinking more along the lines of what is technically possible, regardless of user behavior. – RockPaperLizard May 4 '15 at 0:58
4

Take a look at Broadcast Actions (scroll down to Standard Broadcast Actions).

All intents that are declared as a Broadcast Action can be received by an app that declared a receiver in its manifest.

There are some broadcasts that require explicit registering in the system (Annotated with You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().).

The most important ones for apps that run code after installing are:

  • ACTION_USER_PRESENT: Sent when the user is present after device wakes up (e.g when the keyguard is gone).
  • ACTION_SCREEN_ON: Broadcast Action: Sent when the device wakes up and becomes interactive.
  • ACTION_SCREEN_OFF: Broadcast Action: Sent when the device goes to sleep and becomes non-interactive.
  • ACTION_MY_PACKAGE_REPLACED: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.
  • ACTION_HEADSET_PLUG: Wired Headset plugged in or unplugged. Same as ACTION_HEADSET_PLUG, to be consulted for value and documentation.
  • ACTION_DATE_CHANGED: The date has changed.

(Source: http://developer.android.com/reference/android/content/Intent.html)

Is it even possible for an app to act as a receiver to an alarm timer so that it automatically gets run every minute?

Yes it is possible using the ACTION_TIME_TICK Broadcast Action. It has to be registered using Context.registerReceiver() but after that the registered service is called every minute.

Many people run App Ops right after installing an app to limit exposure. But it seems like it would be almost trivial for an app to run code immediately after being installed.

Is this correct, or are there safeguards to prevent this?

Your assumption is quite correct. Theoretically any app could declare a Broadcast Receiver to receive one of the above mentioned Broadcast Actions and start executing some code. This is even more crucial when updating an app because of the ACTION_MY_PACKAGE_REPLACED broadcast to execute code right after the update is completed. That's the reason many people use AppOps to prevent apps from running if one of the more often sent broadcasts are sent.

  • 1
    Good answer, but what about the other parts of the OP's question? As well as you tackled the Broadcast side of it, the answer remains incomplete. No disrespect intended. – HasH_BrowN May 4 '15 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.