Java Android检查WIFI状态(断开连接或用户更改WIFI)
我有没有办法标记WIFI连接是否断开/断开,或者用户是否实际更改了WIFI网络 我需要我的应用程序执行以下操作: 连接到WIFI XYZ,如果XYZ断开连接(标志1)或断开,则重新连接到XYZ。 但如果用户更改为另一个wifi BTOpen(标志2),则允许连接并停止我的服务。 如果用户再次连接到XYZ,则再次启动循环 到目前为止,我得到的是:Java Android检查WIFI状态(断开连接或用户更改WIFI),java,android,service,connection,wifi,Java,Android,Service,Connection,Wifi,我有没有办法标记WIFI连接是否断开/断开,或者用户是否实际更改了WIFI网络 我需要我的应用程序执行以下操作: 连接到WIFI XYZ,如果XYZ断开连接(标志1)或断开,则重新连接到XYZ。 但如果用户更改为另一个wifi BTOpen(标志2),则允许连接并停止我的服务。 如果用户再次连接到XYZ,则再次启动循环 到目前为止,我得到的是: <!-- WIFI Receiver --> <receiver android:name=".ReceiverWif
<!-- WIFI Receiver -->
<receiver android:name=".ReceiverWifi" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".ServiceWifiMonitor" />
<receiver android:name=".ServiceController" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
服务监视器:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand > Received start id " + startId + ": " + intent);
objHandler.postDelayed(mTasks, 1000);
return START_STICKY;
}//end onStartCommand
private Runnable mTasks = new Runnable() {
public void run() {
if(myApplication.getWifiByChoise().equals("XYZ") && NetworkUtil.isXYZAvailable(context)) {
try
{
//Get the numbers of Reconnection
int count = myApplication.getReconnectedCount();
if(!NetworkUtil.isWifiConnected(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
if(!NetworkUtil.isConnectedToXYZ(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
} catch (Exception e) {e.printStackTrace();}
}
else { stopSelf(); }
int ms_interval = 3000;
objHandler.postDelayed(mTasks, ms_interval);
}
};//end Runnable mTasks
我的应用程序的问题在于:
它撞坏了设备,好像它吃掉了所有的内存。
有时,当wifi XYZ断开连接时,它将无法再次连接,如果用户更改为其他wifi,它将不允许连接
我真的很感谢你的帮助。多谢各位
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand > Received start id " + startId + ": " + intent);
objHandler.postDelayed(mTasks, 1000);
return START_STICKY;
}//end onStartCommand
private Runnable mTasks = new Runnable() {
public void run() {
if(myApplication.getWifiByChoise().equals("XYZ") && NetworkUtil.isXYZAvailable(context)) {
try
{
//Get the numbers of Reconnection
int count = myApplication.getReconnectedCount();
if(!NetworkUtil.isWifiConnected(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
if(!NetworkUtil.isConnectedToXYZ(context))
{
NetworkUtil.connectToXYZ(context);
myApplication.setisReconnect(true);
myApplication.setReconnectedCount(count++);
}
} catch (Exception e) {e.printStackTrace();}
}
else { stopSelf(); }
int ms_interval = 3000;
objHandler.postDelayed(mTasks, ms_interval);
}
};//end Runnable mTasks