不运行时如何启动Android应用 [英] How to launch an Android App when it is not running
问题描述
我有兴趣弄清楚如何在未运行的情况下启动Android应用程序.假设该应用程序已经安装在用户设备上,但该应用程序未运行.当用户访问特定的wifi网络时,我希望该应用自动启动.
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.
如何实现?您是否建议使用在后台运行的Android服务来启动该应用程序? Android服务是否始终在后台运行?这样会给手机带来沉重的负担,以不断检查Wifi连接的状态吗?
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?
谢谢! 娜塔莉(Natalie)
Thank you! Natalie
推荐答案
您正在寻找的是广播接收器.您需要创建一个侦听网络更改事件,该事件将在电话连接到Wifi时触发.无论应用是否运行,此广播接收器都会触发.您必须在<application>
节点
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>
然后在您创建的BroadcastReceiver中可以获取所连接的wifi网络的名称(如果有的话):
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() .
注意:android.net.conn.CONNECTIVITY_CHANGE
会在发生任何连接更改时触发,因此您需要检查是否只是连接到wifi或做了其他事情.
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.
这篇关于不运行时如何启动Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!