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

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. 遞歸地;遞迴地

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章