https://developer.android.com/training/building-connectivity.html

本文介绍了Android平台上的无线通信技术,特别是网络服务发现(NSD)功能,它允许应用发现并连接到本地网络上的其他设备和服务。文章还讨论了如何优化应用的数据使用,包括检测网络类型和响应数据节省模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wirelessly 无线地

Besides enabling communication with the cloud, Android’s wireless APIs also enable communication with other devices on the same local network, and even devices which are not on a network, but are physically nearby. The addition of Network Service Discovery (NSD) takes this further by allowing an application to seek out a nearby device running services with which it can communicate. Integrating this functionality into your application helps you provide a wide range of features, such as playing games with users in the same room, pulling images from a networked NSD-enabled webcam, or remotely logging into other machines on the same network

除了与云通信之外,Android的无线api还可以在本地网络上与其他设备进行通信,甚至在网络上的设备也能与其他设备进行通信,但在物理上就在附近。网络服务发现(NSD)的加入使得应用程序可以寻找附近的设备运行服务来进行通信。将此功能集成到应用程序中,可以帮助您提供广泛的功能,比如在同一个房间中与用户进行游戏,从网络支持的网络摄像头中提取图像,或者远程登录到同一网络上的其他机器上(来自有道)

https://developer.android.com/training/connect-devices-wirelessly/nsd.html

Adding NSD to your app allows your users to identify other devices on the local network that support the services your app requests. This is useful for a variety of peer-to-peer applications such as file sharing or multi-player gaming. Android’s NSD APIs simplify the effort required for you to implement such features

将NSD添加到应用程序中,可以让用户在本地网络上识别支持应用程序请求的其他设备。这对于各种对等应用程序(如文件共享或多玩家游戏)非常有用。Android的NSD api简化了实现这些功能所需的工作。

syntax n. 语法;句法;有秩序的排列
asynchronous adj. [电] 异步的;不同时的;不同期的

This lesson describes how to write applications that have fine-grained control over their usage of network resources. If your application performs a lot of network operations, you should provide user settings that allow users to control your app’s data habits, such as how often your app syncs data, whether to perform uploads/downloads only when on Wi-Fi, whether to use data while roaming, and so on. With these controls available to them, users are much less likely to disable your app’s access to background data when they approach their limits, because they can instead precisely control how much data your app uses.

本课描述如何编写对网络资源的使用有细粒度控制的应用程序。如果您的应用程序执行了大量的网络操作,您应该提供用户设置,允许用户控制您的应用程序的数据习惯,比如您的应用程序同步数据的频率,是否在wi - fi上执行上传/下载,是否在漫游时使用数据,等等。有了这些控制,当用户接近他们的限制时,他们就不太可能禁用你的应用程序访问后台数据.

检测用户使用的是流量还是WiFi

private static final String DEBUG_TAG = "NetworkStatusExample";
...
ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);

Setting up a BroadcastReceiver that gets called unnecessarily can be a drain on system resources. The sample application registers the BroadcastReceiver NetworkReceiver in onCreate(), and it unregisters it in onDestroy(). This is more lightweight than declaring a in the manifest. When you declare a in the manifest, it can wake up your app at any time, even if you haven’t run it for weeks. By registering and unregistering NetworkReceiver within the main activity, you ensure that the app won’t be woken up after the user leaves the app. If you do declare a in the manifest and you know exactly where you need it, you can use setComponentEnabledSetting() to enable and disable it as appropriate.

lightweight n. 轻量级选手;无足轻重的人
动态注册BroadcastReceiver相对于在Manifest中注册来说更加轻量级。如果你在Manifest中注册,它可以随时唤醒你的app,即使你一周都没有启动你的app。

Optimizing adj. 最佳的

Over the life of a smartphone, the cost of a cellular data plan can easily exceed the cost of the device itself. From Android 7.0 (API level 24), users can enable Data Saver on a device-wide basis in order to optimize their device’s data usage, and use less data. This ability is especially useful when roaming, near the end of the billing cycle, or for a small prepaid data pack.

When a user enables Data Saver in Settings and the device is on a metered network, the system blocks background data usage and signals apps to use less data in the foreground wherever possible. Users can whitelist specific apps to allow background metered data usage even when Data Saver is turned on.

从Android 7.0(API level 24)中,用户可以在设备的基础上实现数据保护,从而优化设备的数据使用,减少数据的使用,当用户在设置里开启数据保护,系统会阻止后台数据的使用,并在可能的情况下使用更少的数据来显示应用程序。用户可以在白名单上特定的应用程序允许后台计量数据的使用,即使是在数据保护打开的时候。

It is considered good practice to limit data usage whenever the device is connected to a metered network, even if Data Saver is disabled or the app is whitelisted. The following sample code uses ConnectivityManager.isActiveNetworkMetered() and ConnectivityManager.getRestrictBackgroundStatus() to determine how much data the app should use

限制数据流量是个好习惯,即使Data Saver是处于不可用的状态或者你的app处于白名单。

检测数据保护方法

ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);
// Checks if the device is on a metered network
if (connMgr.isActiveNetworkMetered()) {
  // Checks user’s Data Saver settings.
  switch (connMgr.getRestrictBackgroundStatus()) {
    case RESTRICT_BACKGROUND_STATUS_ENABLED:
    // Background data usage is blocked for this app. Wherever possible,
    // the app should also use less data in the foreground.

    case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
    // The app is whitelisted. Wherever possible,
    // the app should use less data in the foreground and background.

    case RESTRICT_BACKGROUND_STATUS_DISABLED:
    // Data Saver is disabled. Since the device is connected to a
    // metered network, the app should use less data wherever possible.
  }
} else {
  // The device is not on a metered network.
  // Use data as required to perform syncs, downloads, and updates.
}

Apps can monitor changes to Data Saver preferences by creating a BroadcastReceiver to listen for ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED and dynamically registering the receiver with Context.registerReceiver(). When an app receives this broadcast, it should check if the new Data Saver preferences affect its permissions by calling ConnectivityManager.getRestrictBackgroundStatus().

通过注册广播监听ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED可以实时的知道是否处于数据限制。

recursively adv. 递归地;递回地

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值