android.app.usage.NetworkStats Java Examples
The following examples show how to use
android.app.usage.NetworkStats.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetworkStatsManagerBytesCollector.java From Battery-Metrics with MIT License | 6 votes |
private void getBytesForType( long[] bytes, int connectivityManagerType, int type, long startTimeMs, long endTimeMs) throws RemoteException { NetworkStats stats = mNetworkStatsManager.querySummary(connectivityManagerType, null, startTimeMs, endTimeMs); while (stats.hasNextBucket()) { stats.getNextBucket(mBucket); int appState = mBucket.getState() == NetworkStats.Bucket.STATE_FOREGROUND ? NetworkBytesCollector.FG : NetworkBytesCollector.BG; bytes[type | RX | appState] += mBucket.getRxBytes(); bytes[type | TX | appState] += mBucket.getTxBytes(); } stats.close(); }
Example #2
Source File: NetUtils.java From Awesome-WanAndroid with Apache License 2.0 | 5 votes |
/** * Given the start time and end time, then you can get the traffic usage during this time. * * @param startTime Start of period. Defined in terms of "Unix time", see * {@link System#currentTimeMillis}. * @param endTime End of period. Defined in terms of "Unix time", see * {@link System#currentTimeMillis}. * @param netType the netWorkType you want to query * @return Number of bytes. */ @RequiresApi(api = Build.VERSION_CODES.M) public static long getNetStats(@NonNull Context context, long startTime, long endTime, int netType) { long netDataReceive = 0; long netDataSend = 0; String subId = null; NetworkStatsManager manager = (NetworkStatsManager) context.getApplicationContext(). getSystemService(Context.NETWORK_STATS_SERVICE); if (manager == null) { return 0; } NetworkStats networkStats = null; NetworkStats.Bucket bucket = new NetworkStats.Bucket(); try { networkStats = manager.querySummary(netType, subId, startTime, endTime); } catch (Exception e) { e.printStackTrace(); } while (networkStats != null && networkStats.hasNextBucket()) { networkStats.getNextBucket(bucket); int uid = bucket.getUid(); if (getAppUid(context) == uid) { netDataReceive += bucket.getRxBytes(); netDataSend += bucket.getTxBytes(); } } return (netDataReceive + netDataSend); }
Example #3
Source File: DataManager.java From AppsMonitor with MIT License | 5 votes |
private Map<String, Long> getMobileData(Context context, TelephonyManager tm, NetworkStatsManager nsm, int offset) { Map<String, Long> result = new HashMap<>(); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { long[] range = AppUtil.getTimeRange(SortEnum.getSortEnum(offset)); NetworkStats networkStatsM; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { networkStatsM = nsm.querySummary(ConnectivityManager.TYPE_MOBILE, null, range[0], range[1]); if (networkStatsM != null) { while (networkStatsM.hasNextBucket()) { NetworkStats.Bucket bucket = new NetworkStats.Bucket(); networkStatsM.getNextBucket(bucket); String key = "u" + bucket.getUid(); Log.d("******", key + " " + bucket.getTxBytes() + ""); if (result.containsKey(key)) { result.put(key, result.get(key) + bucket.getTxBytes() + bucket.getRxBytes()); } else { result.put(key, bucket.getTxBytes() + bucket.getRxBytes()); } } } } } catch (RemoteException e) { e.printStackTrace(); Log.e(">>>>>", e.getMessage()); } } return result; }
Example #4
Source File: NetworkUsageStatsFragment.java From android-testdpc with Apache License 2.0 | 4 votes |
@Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.network_usage_stats, container, false); getActivity().getActionBar().setTitle(R.string.data_usage); mPackageManager = getActivity().getPackageManager(); mNetstatsManager = (NetworkStatsManager)getActivity().getSystemService( Context.NETWORK_STATS_SERVICE); mListData = new ArrayList<>(); mListAdapter = new ArrayAdapter<List<NetworkStats.Bucket>>(getActivity(), R.layout.data_usage_item, android.R.id.title, mListData) { @Override public View getView(int position, View convertView, ViewGroup parent) { View appView = convertView; if (convertView == null) { appView = inflater.inflate(R.layout.data_usage_item, parent, false); final TextView title = (TextView) appView.findViewById(android.R.id.title); final TextView summary = (TextView) appView.findViewById(android.R.id.summary); final TextView state = (TextView) appView.findViewById(R.id.state); ImageView imageView = (ImageView) appView.findViewById(android.R.id.icon); appView.setTag(new ViewHolder(title, summary, state, imageView)); } List<NetworkStats.Bucket> item = getItem(position); bindView(appView, item); return appView; } }; mQuerySpinner = (Spinner) view.findViewById(R.id.query_type_spinner); mQuerySpinner.setOnItemSelectedListener(this); mExplanation = (TextView) view.findViewById(R.id.explanation); mStartDate = getTodayPlus(0, 0); mEndDate = getTodayPlus(Calendar.DAY_OF_MONTH, 1); mStartDateButton = (Button) view.findViewById(R.id.start_date_button); if (mStartDateButton != null) { mStartDateButton.setOnClickListener(this); } mEndDateButton = (Button) view.findViewById(R.id.end_date_button); if (mEndDateButton != null) { mEndDateButton.setOnClickListener(this); } updateButtonsText(); mDataUsageSummary = (TextView) view.findViewById(R.id.data_usage_summary); mDataUsageList = (ListView) view.findViewById(android.R.id.list); mDataUsageList.setAdapter(mListAdapter); mAppHistoryList = (ListView) view.findViewById(R.id.app_history); return view; }
Example #5
Source File: NetworkUsageStatsFragment.java From android-testdpc with Apache License 2.0 | 4 votes |
private void bindView(View appView, List<NetworkStats.Bucket> item) { final ViewHolder viewHolder = (ViewHolder) appView.getTag(); final TextView title = viewHolder.title; final TextView summary = viewHolder.summary; final TextView state = viewHolder.state; final ImageView imageView = viewHolder.image; final Resources res = getResources(); Drawable icon = res.getDrawable(android.R.drawable.ic_dialog_info, null); NetworkStats.Bucket bucket = item.get(0); final int uid = bucket.getUid(); switch (uid) { case NetworkStats.Bucket.UID_REMOVED: { title.setText(R.string.network_stats_uid_removed); } break; case NetworkStats.Bucket.UID_TETHERING: { title.setText(R.string.network_stats_uid_tethering); } break; case android.os.Process.SYSTEM_UID: { title.setText(R.string.network_stats_uid_system); } break; default: { title.setText(getString(R.string.network_stats_uid, uid)); icon = mPackageManager.getDefaultActivityIcon(); final String[] packageNames = mPackageManager.getPackagesForUid(uid); final int length = packageNames != null ? packageNames.length : 0; try { if (length == 1) { final String pkgName = packageNames[0]; final ApplicationInfo info = mPackageManager.getApplicationInfo(pkgName, 0 /* no flags */); if (info != null) { title.setText(info.loadLabel(mPackageManager)); icon = info.loadIcon(mPackageManager); } } else { for (int i = 0; i < length; i++) { final String packageName = packageNames[i]; final PackageInfo packageInfo = mPackageManager.getPackageInfo( packageName, 0 /* no flags */); final ApplicationInfo appInfo = mPackageManager.getApplicationInfo( packageName, 0 /* no flags */); if (appInfo != null && packageInfo != null) { if (packageInfo.sharedUserLabel != 0) { title.setText(mPackageManager.getText(packageName, packageInfo.sharedUserLabel, packageInfo.applicationInfo)); icon = appInfo.loadIcon(mPackageManager); } } } } } catch (PackageManager.NameNotFoundException e) { // keep the default activity icon } } } imageView.setImageDrawable(icon); final int bucketsCount = item.size(); if (bucketsCount == 1) { summary.setText(formatSize(bucket.getRxBytes() + bucket.getTxBytes())); switch (bucket.getState()) { case NetworkStats.Bucket.STATE_FOREGROUND: { state.setText(R.string.network_stats_foreground_state); } break; case NetworkStats.Bucket.STATE_DEFAULT: { state.setText(R.string.network_stats_default_state); } break; case NetworkStats.Bucket.STATE_ALL: { state.setText(R.string.network_stats_combined_state); } break; } } else { summary.setText(getString(R.string.network_stats_items, bucketsCount)); state.setText(R.string.network_stats_combined_state); } }