我找到了关于处理屏幕OFF和屏幕ON意图的好教程:http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
但我希望屏幕每隔5分钟关闭一次,如果已连接,则检查wifi
ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
wifi.isConnected()
我想使用alarm manager
http://developer.android.com/reference/android/app/AlarmManager.html但我不知道如果连接与否则每5分钟检查一次。
答案 0 :(得分:2)
你的方法很糟糕,计时器是多余的 只需为WifiManager.NETWORK_STATE_CHANGED_ACTION创建广播接收器。
答案 1 :(得分:2)
首先声明一个如下的Timer:
Timer timer = new Timer();
然后将计时器的参数分配如下:
timer.scheduleAtFixedRate(new checkWifi, 0,50000);
然后定义checkWifi任务,如下所示:
class checkWifi extends TimerTask {
@Override
public void run() {
//here put the code that will check if the wifi is connected or not
ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
wifi.isConnected()
}
};