开发者

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

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.


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.


You should be able to follow the example set out here, and adapt it to your situation. That should be cleaner than having to loop through the packages everytime:

Example of How to bind an object to a spinner.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜