Search code examples
androidandroid-intentandroid-serviceandroid-pendingintentandroid-location

Can an activity be invoked when the app is not opened from some pending intent without using notification or button clicks?


Note: I haven't coded anything but looking for a structure/keywords or ideas how it can be achieved.

Use Case: I have an activity which basically shows some display/data/or does some operation etc when opened manually from button click, notification click etc. Now I want to display the same stuff when a particular condition is satisfied [like my geo location matches that I force to be my home or user home].
1. without user clicking any notification,
2. without opening the activity,
3. without clicking buttons, and
4. without opening the app.

So in short, I want my app to be launched when I am home or reach a geo fence, is it achievable?

I am aware of Pending Intents, but seems they need the activity to be opened once and register the intent, let me know if this is the only way to go.

Kindly help!


Solution

  • when a particular condition is satisfied

    This is not possible in general. For example, Android has nothing built-in with the ability to do something automatically based upon when your neighbor's cousin's daughter posts something on Stack Overflow. Even though that is a particular condition that could be satisfied, Android does not know your neighbor, nor your neighbor's relatives, nor the neighbor's cousin's children, nor the online actions of your neighbor's cousin's daughter.

    There are specific things that Android can handle that offer the ability to give you control for related conditions, such as AlarmManager for getting control at certain points in time, or JobScheduler for getting control periodically when the environment is proper (e.g., we have connectivity and are on a charger).

    Your question uses a geofence as an example. The Play Services SDK offers geofence APIs that you can use, that can give you control when the user enters a particular region.

    I want my app to be launched when I am home or reach a geo fence, is it achievable?

    You are welcome to call those geofence APIs, providing a PendingIntent that points to a WakefulBroadcastReceiver, which in turn points to an IntentService, where you can go do something.

    If by "my app is launched", you mean bring up one of your activities, while that is technically possible using an activity PendingIntent, you may make your users very unhappy for interrupting what they are doing (e.g., collecting Pokémon). Please consider using a Notification instead.

    I am aware of Pending Intents, but seems they need the activity to be opened once and register the intent

    Nothing of your app will ever run until the user manually runs your app (e.g., taps on your home screen launcher icon) or until something else uses an explicit Intent to start one of your components. At that point, you can call the geofence APIs, assuming that you somehow know where the user's home is.

    Most likely, you need the user to spend time in the activity simply to collect the data from the user about where they want the geofences to be established.