Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPo Kai Wang
470 PointsMonitor method and Service question(Android studio)
Hi I used trafficstats(getUidRxBytes(uid)) to get thet network usage, and create a view holder to hold the information and display in list view. I would like to use handler in the Service to get the data in every hours. Below shows the two methods, but I dont know how to combined them together?
In Service
@Override
public void onCreate(){
super.onCreate();
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
//float real_data = (float)msg.arg1;
if(msg.arg1 > 1024 ){
System.out.println(msg.arg1 / 1024 + "kb/s");
}
else{
System.out.println(msg.arg1 + "b/s");
}
}
}
};
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() { mHandler.postDelayed(mRunnable, count * 10000);
Message msg = mHandler.obtainMessage();
msg.what = 1;
msg.arg1 =rxx;//getNetSpeed();
mHandler.sendMessage(msg);
}}
In Activity
private List apps = new ArrayList<ApplicationBean>();
private ListView mListView;
public void getAppTrafficStatList() {
PackageManager pm = getPackageManager();
List<PackageInfo> pinfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES |PackageManager.GET_PERMISSIONS);
for (PackageInfo info : pinfos) {
String[] pers = info.requestedPermissions;
if (pers != null && pers.length > 0) {
for (String per : pers)
if ("android.permission.INTERNET".equals(per)) {
int uid = info.applicationInfo.uid;
String lable = (String) info.applicationInfo.loadLabel(pm);
Drawable icon = null;
try {
icon = pm.getApplicationIcon(info.packageName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
long rx = TrafficStats.getUidRxBytes(uid);
long tx = TrafficStats.getUidTxBytes(uid);
int rxx= (int) rx;
ApplicationBean app = new ApplicationBean();
app.setName(lable);
app.setIcon(icon);
app.setRx(rx);
app.setTx(tx);
apps.add(app);}}
I try to remove the gettrafficstatlist method and move them to the Service file, but then I can not use the private List apps = new ArrayList<ApplicationBean>(); in the Service. I am very confused, can someone help please.