Reputation: 5642
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
Upvotes: 0
Views: 503
Reputation: 4798
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.
Upvotes: 1
Reputation: 5107
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>
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.
Upvotes: 2