简体   繁体   English

限制Android同步到WIFi

[英]Restricting Android syncing to WIFi

我是Android的新手,我正在尝试编写SyncAdapter - 是否可以将SyncAdapter限制为仅在网络为WiFi(而不是3G等)时才执行同步?

you can make check for wifi as 你可以检查无线网络

    ConnectivityManager cm = (ConnectivityManager) YourActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();

    if(info.getType()==ConnectivityManager.TYPE_WIFI)
    {
        System.out.println("WiFi Connected");
    }
    else
    {
            System.out.println("WiFi not connected");
    }

and also add permission in manifest as 并在清单中添加权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The answer above is good, but I would add a HttpHead-Request (HTTPGet is also possible but you receive more data than needed) that is calling eg google.com so you can be sure that you are really connected to the internet. 上面的答案是好的,但我会添加一个HttpHead-Request(HTTPGet也可以,但你收到的数据超过需要的数据),例如google.com,这样你就可以确定你真正连接到了互联网。 The above posted solution just checks if you have network access but does not guarantee that you have inernet access. 上面发布的解决方案只检查您是否具有网络访问权限,但不保证您具有inernet访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM