Android:每当互联网连接时在后台运行

时间:2016-02-12 05:53:06

标签: android

Hello Guys我只有一个简单的问题,即如何在后台连续在后台运行一些代码块,

情况是检查互联网是否连接,因此它会在服务器上传文件。(这将在后台连续运行。)

我得到的东西就像BroadcastReceiver,但我认为它只是检查互联网连接。

如果有人提供一段代码并让我理解,那将会有很大帮助......

谢谢!!!!!

1 个答案:

答案 0 :(得分:1)

我会给你一个例子

您可以通过此

轻松检查您的连接

创建 NetworkUtils 类:

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;
    }
}

创建 NetworkConnectivityCheck 类:

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);
    }
}

主要活动:

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);
    }
}