1

I depending on the current network status , Change image.

WI-FI , ethernet, unable network.

I want implement Asynctask. but I don't know how can I implement.

this code When ethernet connected.

thanks.

public class EthernetImage extends AsyncTask<ImageView, Void, ImageView> {

    ImageView ethernet;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ethernet = (ImageView) findViewById(R.id.EthernetConnected);
    }

    @Override
    protected ImageView doInBackground(Void... params) {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        int m_iNetworkType = (activeNetwork == null) ? -1 : activeNetwork.getType();
        if (m_iNetworkType == cm.TYPE_WIFI) {
            ethernet.setVisibility(View.INVISIBLE);
        } else if (m_iNetworkType == cm.TYPE_ETHERNET) {
            ethernet.setVisibility(View.VISIBLE);
        } else {
            ethernet.setVisibility(View.INVISIBLE);
        }
        return ethernet;
    }

    @Override
    protected void onPostExecute(ImageView result) {
        super.onPostExecute(result);
        if (result != null);
        ethernet.setImageResource(R.drawable.ethernetimage);
    }
}
ELITE
  • 5,815
  • 3
  • 19
  • 29
  • do you want to change image in your imageView according to if else conditions. – ELITE Mar 03 '16 at 02:42
  • How can I condition ? –  Mar 03 '16 at 02:45
  • change return type of `AsyncTask` to boolean or anything else than `ImageVIew`. and check same condition in `onPostExecute` method and change image accordingly.. – ELITE Mar 03 '16 at 02:51
  • but annoying , please give me example code , sorry :); –  Mar 03 '16 at 02:56

1 Answers1

0

Change defination of AsyncTask<ImageView, Void, ImageView> to AsyncTask<Void, Void, String> and change methods accordingly

protected String doInBackground(Void... params) {
    if (m_iNetworkType == cm.TYPE_WIFI) {
        return "wifi";
    } else if (m_iNetworkType == cm.TYPE_ETHERNET) {
        return "ethernet";
    } else {
        return null;
    }
}

and check condition in

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    if (result != null) {
        if("ethernet".equals(result)) {
            ethernet.setImageResource(R.drawable.ethernetimage);
        } else if("wifi".equals(result)) {
            ethernet.setVisibility(View.INVISIBLE);
        }
    } else {
        ethernet.setVisibility(View.INVISIBLE);
    }
}
ELITE
  • 5,815
  • 3
  • 19
  • 29
  • public class EthernetImage extends AsyncTask -> class 'EthernetImage ' must either be declared abstract or implement abstract method 'doInBackground(params) in 'asynctask' error –  Mar 03 '16 at 05:04
  • Change solution.check it out. – ELITE Mar 03 '16 at 05:06
  • I try AsyncTask to –  Mar 03 '16 at 05:06
  • hide imageView in else condition `ethernet.setVisibility(View.INVISIBLE);` – ELITE Mar 03 '16 at 06:06
  • add question. recently, new EthernetImage().execute() call on onresume() I want without changing the screen, when the network changes, change the image. I think create receiver class. is it right? –  Mar 03 '16 at 06:49
  • Yes, you have to create `BroadcastReceiver` to check if network state changed and is online, and then change image in image view. – ELITE Mar 03 '16 at 06:56
  • how use findViewById in receiver class –  Mar 03 '16 at 07:11
  • read the answer. It'll help you http://stackoverflow.com/questions/25215878/how-to-update-the-ui-of-activity-from-broadcastreceiver – ELITE Mar 03 '16 at 07:19
  • new IntentFilter("YourIntentAction") comment. I don't know ("YourIntentAction") what I should IntentAction write? –  Mar 03 '16 at 07:27
  • check this also http://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app – ELITE Mar 03 '16 at 07:34
  • ohhhhhh... very hard :(;; –  Mar 03 '16 at 07:49
  • what typing this command? I WI-FI receiver registerReceiver(rssiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION)); but I don't know ethernet receiver , unable network receiver what typing 'here type'? –  Mar 03 '16 at 08:33