-1

I was trying to get list of all application in android and calculating bytes send and recieved but the code below gives pickage names not application names:

    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
        if (runningProcesses != null && runningProcesses.size() > 0) {
        // Set data to the list adapter



                setListAdapter(new ListAdapter(this, runningProcesses));


    }
     else {
        // In case there are no processes running 
        Toast.makeText(getApplicationContext(), "No application is running", Toast.LENGTH_LONG).show();
    }





    }   


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    long send       = 0;
    long recived    = 0;

    long wr,ws,mr,ms =0;

    // Get UID of the selected process/ application
    int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid;


    // Get traffic data
    recived = TrafficStats.getUidRxBytes(uid);
    send = TrafficStats.getUidTxBytes(uid);

    // Get Mobile data:
     mr =  TrafficStats.getTotalRxBytes();
     ms = TrafficStats.getMobileTxBytes();

    //GetWifiDataA:

    wr = (TrafficStats.getTotalRxBytes())- (TrafficStats.getMobileRxBytes());

    ws =  (TrafficStats.getTotalTxBytes())- (TrafficStats.getMobileTxBytes());

    // Display data
    Toast.makeText(getApplicationContext(), " \nUID " + uid + " details.. \nWifi Send:  " +ws /1000+" \n Wifi Received: " +wr/1000+"kB"+ "\n Mobile Send: "+ms/1000+" kB"+"\n Mobile Received: "+mr/1000+"kB",Toast.LENGTH_LONG).show();
}

solved

the code with package manager solved this issue:

 packageManager = getPackageManager();
        List<PackageInfo> packageList = packageManager
                .getInstalledPackages(PackageManager.GET_META_DATA);
 apkList = (ListView) findViewById(R.id.applist);
        apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));


on next activity:


        int app_uid = packageInfo.applicationInfo.uid;

        long send       = 0;
        long recived    = 0;
    // Get traffic data
                recived = TrafficStats.getUidRxBytes(app_uid);
                send = TrafficStats.getUidTxBytes(app_uid);

 // APP name
        appLabel.setText(getPackageManager().getApplicationLabel(
                packageInfo.applicationInfo));

        // package name
        packageName.setText(packageInfo.packageName);

        // version name
        version.setText(packageInfo.versionName);

        // received
        andVersion.setText(Long
                .toString(recived));

        // send
        path.setText(Long.toString(send));
3
  • 2
    see this link to get application name [1]: stackoverflow.com/questions/5841161/… [2]: stackoverflow.com/questions/11229219/… Commented Nov 23, 2013 at 15:45
  • please review the edited question and reply.. thanks alot Commented Nov 24, 2013 at 6:59
  • But this approach lists only installed applications like whatsapp, tango etc .. I want all runnin applications like mapsfacebook, browser. What did you just say ? :O Aren't running apps are installed as well? Man !! I guess you found a flaw in Android, report it ASAP. Commented Nov 24, 2013 at 7:10

1 Answer 1

4

Write your code like this..here it returns the arraylist of all installed application names..

public ArrayList<String> getAppnames() {
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> activities = getPackageManager().queryIntentActivities( mainIntent, 0);
    ArrayList<String>appList=new ArrayList<String>();
    for (ResolveInfo resolveInfo : activities) {
        appList.add(resolveInfo.loadLabel(getPackageManager()).toString());
    }
    return appList;     
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.