Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get uid value of an android application from a list displayed in a spinner?

Tags:

android

I am developing a small application which displays the installed application as a list in a spinner. Only the application name is displayed as spinner values. Whenever I select an application from spinner I need to retrieve the UID of the selected application. How can I implement the function?

The following is my code for getting the application name from the spinner

appspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int app_pos, long app_id) {
            // TODO Auto-generated method stub
            String app_selected=parent.getItemAtPosition(app_pos).toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            return;
        }
    });

The installed application is stored in a list using the PackageManager class and using PackageInfo class I am getting the name of the application.

like image 790
Unnikrishnan Avatar asked Jul 29 '11 16:07

Unnikrishnan


People also ask

How to get UID of application in android?

Use android. os. Process. myUid() to get the calling apps UID directly.


1 Answers

You will need to use PackageManager to get the package information about whatever app you select in the list. I haven't done this with a Spinner but i'm sure it should work the same as it did in my ListView.

appspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int app_pos, long app_id) {
        // TODO Auto-generated method stub
        String app_selected=parent.getItemAtPosition(app_pos).toString();

        final PackageManager pm = getPackageManager();
        //get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(
                PackageManager.GET_META_DATA);
        int UID;
        //loop through the list of installed packages and see if the selected
        //app is in the list
        for (ApplicationInfo packageInfo : packages) {
            if(packageInfo.packageName.equals(app_selected)){
                //get the UID for the selected app
                UID = packageInfo.uid;
                break; //found a match, don't need to search anymore
            }

        }

        //Do whatever with the UID
        Log.i("Check UID", "UID is: " + UID);               

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        return;
    }
});

You might want to see how packageName returns the packageName so you can try to match it with whatever was selected.

hope this points you in the right direction and helps you out. Good Luck.

like image 67
Wolfcow Avatar answered Oct 06 '22 00:10

Wolfcow