How to simulate network issues on a Android device?

I need to test my app in following scenario:

  • connect device to wifi (with the internet).
  • simulate internet interruption (lack of the internet but the device must stay connected to the wifi network).
  • after few seconds the internet need to be active again.
  • I could simulate this pulling the adsl cable out of my ruter, but then it won't reconnect fast enough. Is this any way to simulate this programmatically?


    If you have a Mac, you can do it with Network Link Conditionner. It's really efficient, see here the step to install it :

    http://nshipster.com/network-link-conditioner/


    If you happen to have another device, you can create a hotspot with it and disallow mobile networks internet. The testing device will be connected to the hotspot, but will not be able to reach anywhere due to lack of Internet. As for the connection there, I think it is faster than restarting a router. However, you have to have another device for this to happen... Otherwise I don't know, and I'd like to know a true answer, as well.


    Try to pass all network requests (I use it with db transactions too) through a android.os.Handler. Use Runnables (better implement your own Runnable class) to implement the actions to perform (add callbacks, logging and other logic in your Runnable class). This way you can easily register listeners in your Runnable class to do whatever sleeping or failures your are interested in.

    Here is some code from my project. The M_ClientRunnable class implements Runnable and has additional methods for registering "cancellators", failure listeners and success listeners.

    class LooperThread extends Thread {
        public Handler mHandler;
    
        public LooperThread() {
            super();
            start();
        }
    
        public void run() {
            Looper.prepare();
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    getLogger().info(TAG, msg.toString());
                }
            };
            Looper.loop();
        }
    
        public boolean post(M_ClientRunnable runnable) {
            if(!isAlive()) {
                start();
            }
            if (mHandler!=null) {
                return mHandler.post(runnable);
            } else {
                return false;
            }
        }
    };
    private LooperThread looper = new LooperThread();
    @Override
    public boolean post(M_ClientRunnable runnable) {
        return looper.post(runnable);
    }
    
    链接地址: http://www.djcxy.com/p/18102.html

    上一篇: 从html中删除角色评论

    下一篇: 如何模拟Android设备上的网络问题?