1

I want to develop an application which will run in the background when the wifi is turned on. The background app will check whether the device is connected to wifi network for every 15 minutes. If it is not connected then turn off the wifi in the device. Now how do I start the application in the background when wifi is turned on?

babybob
  • 444
  • 6
  • 22

3 Answers3

1

You need to create service which runs in background and you can write logic there where it checks whether wifi is available or not after every 15 mins.

If it detect wifi then run your background operation in the same service.

you can refer this http://stacktips.com/tutorials/android/android-service-example

Also you can detect the broadcast while system detects wifi.

public class WifiReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {     
    ConnectivityManager conMan = (ConnectivityManager) 
   context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = conMan.getActiveNetworkInfo();
    if (netInfo != null && netInfo.getType() == 
   ConnectivityManager.TYPE_WIFI) 
        Log.d("WifiReceiver", "Have Wifi Connection");
    else
        Log.d("WifiReceiver", "Don't have Wifi Connection");    
}   
};
sourabh kaushik
  • 523
  • 4
  • 20
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

You must use a Broadcast receiver. The event which starts the Broadcast receiver is the change of the connectivity.

To let it starts in background you can start a service when the Broadcast Receiver reveals this change of connectivity

Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62
0

You need to use BroadcastReceiver to get wifi event.

Releated answer explains how to do it pretty well.

Community
  • 1
  • 1
Efe Budak
  • 659
  • 5
  • 27