如何在runnable中更改变量值?或按钮按下如何重置数据计数器?

时间:2018-06-08 18:29:25

标签: android github traffic network-traffic

我正在使用以下链接中的trafficstats app: datastats app github

运行时上面的应用程序会在设备启动时提供流量数据。 我需要在过去一小时左右的数据流量。

那么有没有办法通过按下按钮将计数器初始化为0?到目前为止,我仍然坚持这个:

public class main extends Activity {

 private TextView tvSupported, tvDataUsageWiFi, tvDataUsageMobile, tvDataUsageTotal;
 private ListView lvApplications;

 private long dataUsageTotalLast = 0;

 ArrayAdapter < ApplicationItem > adapterApplications;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  tvSupported = (TextView) findViewById(R.id.tvSupported);
  tvDataUsageWiFi = (TextView) findViewById(R.id.tvDataUsageWiFi);
  tvDataUsageMobile = (TextView) findViewById(R.id.tvDataUsageMobile);
  tvDataUsageTotal = (TextView) findViewById(R.id.tvDataUsageTotal);

  if (TrafficStats.getTotalRxBytes() != TrafficStats.UNSUPPORTED && TrafficStats.getTotalTxBytes() != TrafficStats.UNSUPPORTED) {
   handler.postDelayed(runnable, 0);


   initAdapter();
   lvApplications = (ListView) findViewById(R.id.lvInstallApplication);
   lvApplications.setAdapter(adapterApplications);
  } else {
   tvSupported.setVisibility(View.VISIBLE);
  }
 }



 public Handler handler = new Handler();
 public Runnable runnable = new Runnable() {
  public void run() {
   //----------------
   final long mob1 = TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes();
   final long tot1 = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes();
   //-----------------
   Button bres = (Button) findViewById(R.id.resetid);
   bres.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
     mob1 = 0;
    }
   });


   //-----------------
   long mobile = mob1;
   long total = tot1;
   tvDataUsageWiFi.setText("" + (total - mobile) / 1024 + " Kb");
   tvDataUsageMobile.setText("" + mobile / 1024 + " Kb");
   tvDataUsageTotal.setText("" + total / 1024 + " Kb");
   if (dataUsageTotalLast != total) {
    dataUsageTotalLast = total;
    updateAdapter();
   }
   handler.postDelayed(runnable, 5000);
  }
 };

 public void initAdapter() {

  adapterApplications = new ArrayAdapter < ApplicationItem > (getApplicationContext(), R.layout.item_install_application) {
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    ApplicationItem app = getItem(position);

    final View result;
    if (convertView == null) {
     result = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_install_application, parent, false);
    } else {
     result = convertView;
    }

    TextView tvAppName = (TextView) result.findViewById(R.id.tvAppName);
    TextView tvAppTraffic = (TextView) result.findViewById(R.id.tvAppTraffic);

    // TODO: resize once
    final int iconSize = Math.round(32 * getResources().getDisplayMetrics().density);
    tvAppName.setCompoundDrawablesWithIntrinsicBounds(
     //app.icon,
     new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(
      ((BitmapDrawable) app.getIcon(getApplicationContext().getPackageManager())).getBitmap(), iconSize, iconSize, true)),
     null, null, null
    );
    tvAppName.setText(app.getApplicationLabel(getApplicationContext().getPackageManager()));
    tvAppTraffic.setText(Integer.toString(app.getTotalUsageKb()) + " Kb");

    return result;
   }
   @Override
   public int getCount() {
    return super.getCount();
   }

   @Override
   public Filter getFilter() {
    return super.getFilter();
   }
  };

  // TODO: resize icon once

  for (ApplicationInfo app:

   getApplicationContext().getPackageManager().getInstalledApplications(0))

  {
   ApplicationItem item = ApplicationItem.create(app);
   if (item != null) {
    adapterApplications.add(item);
   }
  }
 }

 public void updateAdapter() {
  for (int i = 0, l = adapterApplications.getCount(); i < l; i++) {
   ApplicationItem app = adapterApplications.getItem(i);
   app.update();
  }

  adapterApplications.sort(new Comparator < ApplicationItem > () {
   @Override
   public int compare(ApplicationItem lhs, ApplicationItem rhs) {
    return (int)(rhs.getTotalUsageKb() - lhs.getTotalUsageKb());
   }
  });
  adapterApplications.notifyDataSetChanged();
 }

}

以上代码是github链接的修改片段。 我尝试通过在runnable中声明一个按钮,但该变量需要声明为final,这导致变量不可变。

0 个答案:

没有答案