1

my listview displays installed apps having the internet permission, it works fine however, i am trying to enable clicking on list items and take the user to the screen where they can uninstall the app, my code works without any force closes but opens that uninstall screen only 0.25 second and goes back. this is because it is not getting the correct package name. How do i fix this?

    public class MainActivity extends Activity {

        ArrayList<String> results = new ArrayList<String>();

         @Override
           protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              getInstalledApps(this);
              ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, results);

              ListView listView = (ListView) findViewById(R.id.mobile_list);
              listView.setAdapter(adapter);

              listView.setOnItemClickListener(new OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position,
                          long id) {
                      String packageName = results.get(position);
                      Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      intent.setData(Uri.parse("package:" + packageName));
                      startActivity(intent);
                  }
              });
         }

          private ArrayList<String> getInstalledApps(Context context) {
            PackageManager packageManager = context.getPackageManager();
            List<PackageInfo> applist = packageManager.getInstalledPackages(0);
            Iterator<PackageInfo> it = applist.iterator();
            while (it.hasNext()) {
                PackageInfo pk = (PackageInfo) it.next();
                if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                    Log.v("system app using internet = ", ""+pk.applicationInfo.loadLabel(packageManager));
                    continue;
                }

                if (PackageManager.PERMISSION_GRANTED == packageManager .checkPermission(Manifest.permission.INTERNET, pk.packageName) ||
                        PackageManager.PERMISSION_GRANTED == packageManager .checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, pk.packageName))
                    results.add("" + pk.applicationInfo.loadLabel(packageManager));
            }

            Log.v("app using internet = ", results.toString());

            return results;
        }
    }

also i would like to take it directly to the uninstall screen instead of taking it to the app info screen.

2 Answers2

0

You are storing the name of the app in your results array instead of the package name, which is different.

Here's an example: the Facebook app has the name Facebook but its package name is com.facebook.katana

When querying the PackageManager, you should not only request pk.applicationInfo.loadLabel(packageManager) but also pk.packageName (in another array, for example).

So your code should be edited the following way:

  1. Under ArrayList<String> results = new ArrayList<String>(); add ArrayList<String> packageNames = new ArrayList<String>();

  2. Under results.add("" + pk.applicationInfo.loadLabel(packageManager)); add packageNames.add(pk.packageName); Use brackets with your if (...) here!

  3. Replace String packageName = results.get(position); with String packageName = packageNames.get(position);

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • im not getting it :( can you post a snipped instead? thanks :D –  Jan 13 '16 at 09:11
  • I've edited my answer – Daniel Zolnai Jan 13 '16 at 09:26
  • i did everything like you said, it works with some apps, however, it also gives wrong positions for some apps, i click on gallery and it is opening kik. –  Jan 13 '16 at 09:33
  • Make sure this if has brackets: `if (PackageManager.PERMISSION_GRANTED...)` and `result.add()` and `packageNames.add()` are both in the brackets – Daniel Zolnai Jan 13 '16 at 09:38
  • they all have some code next to them in brackets , just like " packageNames.add(pk.packageName);" –  Jan 13 '16 at 09:45
  • and it is still not working :C –  Jan 13 '16 at 09:45
  • It should look like this: `if (PackageManager.PERMISSION_GRANTED == packageManager .checkPermission(Manifest.permission.INTERNET, pk.packageName) || PackageManager.PERMISSION_GRANTED == packageManager .checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, pk.packageName)) { results.add("" + pk.applicationInfo.loadLabel(packageManager)); packageNames.add(pk.packageName); } }` – Daniel Zolnai Jan 13 '16 at 10:01
  • awesome. im accepting this answer, ill upvote if you tell me how to set a similar image on the left of every list item (like ic_launcher) :D –  Jan 13 '16 at 10:31
  • Put an ImageView in your list item and then use `Drawable icon = getPackageManager().getApplicationIcon(pk.packageName);` to set it on the list item. If you are stuck again, create a new StackOverflow question. – Daniel Zolnai Jan 13 '16 at 11:37
  • Sorry but this is out of scope of the question, and I also got other work to do :) – Daniel Zolnai Jan 13 '16 at 12:32
0
    Try this
    ArrayList<AppInfodata> appsDataList = new ArrayList<>();
    public void getAllApps() {

            PackageManager packageManager = getPackageManager();
            List apps = packageManager
                    .getInstalledPackages(PackageManager.SIGNATURE_MATCH);
            if (apps != null && !apps.isEmpty()) {

                for (int i = 0; i < apps.size(); i++) {
                    PackageInfo p = (PackageInfo) apps.get(i);
                    ApplicationInfo appInfo = null;
                    try {
                        appInfo = packageManager.getApplicationInfo(p.packageName,
                                0);

                        AppInfodata app = new AppInfodata();
                        app.appname = p.applicationInfo.loadLabel(packageManager)
                                .toString();
                        app.packagename = p.packageName;

                      app.icon = p.applicationInfo.loadIcon(packageManager);


                            appsDataList.add(app);
                        }

                    } catch (NameNotFoundException e) {
                        e.printStackTrace();
                    }
                }

                // sort the list of applications alphabetically

            }
            public class AppInfodata {
        String appname;
        String packagename;

        Drawable icon;


    }

class AppListAdapter extends ArrayAdapter<AppInfoData>
{
AppListAdapter(Context context,int resource,ArrayList<AppInfoData> datalist)
{
super(context,resource,datalist);
}
getView(.....)
{
AppInfoData data =getItem(position);
}

}

Rohit Heera
  • 2,709
  • 2
  • 21
  • 31