How to launch an Android App when it is not running

被刻印的时光 ゝ 提交于 2019-12-08 10:28:31

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27490902/how-to-launch-an-android-app-when-it-is-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!