0

I am interested in figuring out how to launch an Android App when it is not running. Lets assume the app is already installed on the user's device, but the app is not running. When the user accesses a particular wifi network, I'd like the app to be launched automatically.

How can this be accomplished? Would you recommend to use an Android Service that is running in the background to launch the app? Does the Android Service run in the background at all times? Would this put a heavy load on the phone to constantly check for the status of the Wifi connection?

Thank you! Natalie

code
  • 5,294
  • 16
  • 62
  • 113

2 Answers2

2

What you are looking for is a BroadcastReceiver. You need to create one to listen for the network change event which will fire when the phone connects to Wifi. This broadcast receiver will fire whether the app is running or not. You must declare it in your AndroidManifest.xml in your <application> node

<receiver android:name="MyNetworkReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
</receiver>

http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges

Then in the BroadcastReceiver you create you can get the name of the wifi network you are connected to if any: http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID() .

Note: android.net.conn.CONNECTIVITY_CHANGE fires for any connectivity change so you need to check to see if you just connected to wifi or did something else.

browep
  • 5,057
  • 5
  • 29
  • 37
  • Please note that the user will still have to run your app at least once manually on 3.0+- apps installed but not run are set into the force stop mode, and receivers will not fire. – Gabe Sechan Dec 15 '14 at 19:04
  • Thank you for this answer @browep ! I will try this and let you know how it goes. By the way, if I am interested in another trigger to launch the app... (for example: if the time is 5am in the morning, launch the app) how would I accomplish this? – code Dec 15 '14 at 19:08
  • AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html – browep Dec 15 '14 at 19:09
1

You should be able to declare a receiver in your manifest that will allow your app to wake-up for network change events. You'll have to wakeup, check the wifi network the have connected to and then launch an activity if it's the correct network.

Read this section, it explains it in detail.

http://developer.android.com/training/basics/network-ops/managing.html#detect-changes

When you declare a in the manifest, it can wake up your app at any time, even if you haven't run it for weeks.

Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • Thank you for this answer @Gary ! I will try this and let you know how it goes. By the way, if I am interested in another trigger to launch the app... (for example: if the time is 5am in the morning, launch the app) how would I accomplish this? – code Dec 15 '14 at 19:08