-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));
user2011302
  • 391
  • 1
  • 4
  • 22
  • 2
    see this link to get application name [1]: http://stackoverflow.com/questions/5841161/get-application-name-from-package-name [2]: http://stackoverflow.com/questions/11229219/android-get-application-name-not-package-name – Shayan Pourvatan Nov 23 '13 at 15:45
  • please review the edited question and reply.. thanks alot – user2011302 Nov 24 '13 at 06: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. – M-Wajeeh Nov 24 '13 at 07:10

1 Answers1

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;     
}
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59