1

我正在使用 NetworkStatsManager 的 querySummary() 方法来计算数据使用情况,它给了我正确的使用方法,但有时它会为此方法抛出 NullPointerException。

UsageTestService 中的错误:

java.lang.NullPointerException: 
at android.os.Parcel.readException (Parcel.java:1626)
at android.os.Parcel.readException (Parcel.java:1573)
at android.net.INetworkStatsSession$Stub$Proxy.getSummaryForAllUid 
(INetworkStatsSession.java:355)
at android.app.usage.NetworkStats.startSummaryEnumeration 
(NetworkStats.java:330)
at android.app.usage.NetworkStatsManager.querySummary 
(NetworkStatsManager.java:166)
at com.test.services.UsageTestService$1.run (UsageTestService.java:172)

我的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            NetworkStats networkStats = null;
                            try {

                                networkStatsManager = (NetworkStatsManager) getApplicationContext().getSystemService(Context.NETWORK_STATS_SERVICE);
                                networkStats = networkStatsManager.querySummary(0, getSubscriberId(UsageTestService.this, ConnectivityManager.TYPE_MOBILE), 0, System.currentTimeMillis());

                            } catch (RemoteException e) {

                                e.printStackTrace();
                            }

                            if(networkStats!=null)
                            {

                                while (networkStats.hasNextBucket()) {

                                    NetworkStats.Bucket bucket = new NetworkStats.Bucket();
                                    networkStats.getNextBucket(bucket);

                                    int uid = bucket.getUid();
                                    double rx1 = bucket.getRxBytes();
                                    double tx1 = bucket.getTxBytes();
                                    int state1 = bucket.getState();
                                    double totalRxTx1 = rx1+ tx1;
                                    }
                                    }
                                    }

会有什么问题?

4

1 回答 1

0

我们偶然发现了同样的问题,这似乎是一个平台错误。目前,在 master 分支中仍未修复。

这是当前平台代码的摘录:

        @Override
        public NetworkStats getSummaryForAllUid(
                NetworkTemplate template, long start, long end, boolean includeTags) {
            try {
                @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
                final NetworkStats stats =
                        getUidComplete().getSummary(template, start, end, accessLevel);
                if (includeTags) {
                    final NetworkStats tagStats = getUidTagComplete()
                            .getSummary(template, start, end, accessLevel);
                    stats.combineAllValues(tagStats);
                }
                return stats;
            } catch (NullPointerException e) {
                // TODO: Track down and fix the cause of this crash and remove this catch block.
                Slog.wtf(TAG, "NullPointerException in getSummaryForAllUid", e);
                throw e;
            }
        }

https://github.com/android/platform_frameworks_base/blob/master/services/core/java/com/android/server/net/NetworkStatsService.java#L572

于 2017-09-11T05:58:41.717 回答