0

I want to make a background service that always runs in background service. When mobile connected with internet via WI-FI it notice me. so please help me.. I am newbie. Thanks.

here is my code-

public class MyService extends Service {
private static final String TAG = "My Service Demo";
Context _context;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");

}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

//Create object for ConnectivityManager class which returns network related info

        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);

//If connectivity object is not null

        if (connectivity != null) {
 //Get network info - WIFI internet access
            NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (info != null) {

//Look for whether device is currently connected to WIFI network

                if (info.isConnected()) {
                    Toast.makeText(_context,"wi fi connected", Toast.LENGTH_SHORT).show();
                }

        }
        }
        }

}

0 Answers0