0

Hello Guys I have just a simple question that how to run some block of code in android continuously in background,

The situation is to check the internet connected or not and accordingly it will upload the files on the server.(This will be running continuously in background.)

I am getting something like BroadcastReceiver, but I think its only to check the internet connection.

It will help a lot if someone give a block of code and make me understand...

Thanks !!!!!

Manish
  • 43
  • 2
  • 12
  • Possible duplicate of [How can I run code on a background thread on Android?](http://stackoverflow.com/questions/15472383/how-can-i-run-code-on-a-background-thread-on-android) – Rakesh Feb 12 '16 at 05:54
  • Listen to the connection broadcast event and start a service from your broadcast receiver, do what ever you need to do inside the service , that should be it. – Niraj Adhikari Feb 12 '16 at 06:02

1 Answers1

2

I'll give you an example

You can easily check your connection by this

Create NetworkUtils class:

public class NetworkUtil {
    public static final int NOT_CONNECTED = 0;
    public static final int WIFI = 1;
    public static final int MOBILE = 2;
    public static int getConnectionStatus(Context context){
        ConnectivityManager connectivityManager =
                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if(networkInfo != null){
            if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
                return WIFI;
            }
            if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE){
                return MOBILE;
            }
        }
        return NOT_CONNECTED;
    }
}

Create NetworkConnectivityCheck class:

public class NetworkConnectivityCheck {
    public boolean internetAvailable = false;
    private BroadcastReceiver networkChangeReceiver;

    NetworkConnectivityCheck(){

        this.networkChangeReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int networkState = NetworkUtil.getConnectionStatus(context);
                if(networkState == NetworkUtil.NOT_CONNECTED){
                    internetAvailable = false;
                    MainActivity.tvStatus.setText("OFFLINE");

                } else if(networkState == NetworkUtil.MOBILE){
                    internetAvailable = true;
                    MainActivity.tvStatus.setText("ONLINE"); // you do something here.
                } else if(networkState == NetworkUtil.WIFI){
                    internetAvailable = true;
                    MainActivity.tvStatus.setText("ONLINE"); // you do something here
                }
            }
        };
    }



    public void register(Context context){
        context.registerReceiver(networkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }
    public void unregister(Context context){
        context.unregisterReceiver(networkChangeReceiver);
    }
}

And main activity:

public class MainActivity extends AppCompatActivity {
    private NetworkConnectivityCheck networkConnectivityCheck;

    public static TextView tvStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvStatus = (TextView)findViewById(R.id.tvStatus);

        networkConnectivityCheck = new NetworkConnectivityCheck();
        networkConnectivityCheck.register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        networkConnectivityCheck.register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        networkConnectivityCheck.unregister(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        networkConnectivityCheck.unregister(this);
    }
}
xxx
  • 3,315
  • 5
  • 21
  • 40
  • 1
    Thank You So Much **Huy Nguyen** it helped me alot, can you please give me one more answer that how to keep application running in background like whatsapp or messanger .... – Manish Feb 12 '16 at 07:25