开发者

Android remove app from listview after deleting it

开发者 https://www.devze.com 2023-03-15 23:55 出处:网络
I have a list of user installed applications. When I click on one to delete it says it gets deleted but when I go back to the listview the icon is still showing in the listview and when I try to click

I have a list of user installed applications. When I click on one to delete it says it gets deleted but when I go back to the listview the icon is still showing in the listview and when I try to click on it it says that the app has been uninstalled. How do I make it get removed from the listview after I have deleted it?

I want to update my listview but unsure how to

Here is some code

public void update() {
    // TODO
    mAppListAdapter.clear();

    Intent aIntent = new Intent(Intent.ACTION_MAIN, null);
    aIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PackageManager aPackageManager = getPackageManager();
    List <ResolveInfo> aList = aPackageManager.queryIntentActivities(aIntent, PackageManager.GET_UNINSTALLED_PACKAGES);





    for( ResolveInfo rInfo : aList ) {
         if (!isSystemPackage(rInfo)) 
        mAppListAdapter.add(rInfo.activityInfo.applicationInfo);
         //for (int n=0;n<aList.size();n++) {           
                //if((aList.get(n).flags & ApplicationInfo.FLAG_SYSTEM)!=1)

        System.out.println("Installed Application开发者_高级运维s "  +  rInfo.activityInfo.applicationInfo.loadLabel(aPackageManager).toString()); 
    }

    if( mListView != null ) {
        mListView.setAdapter( mAppListAdapter );



    }
    }


queryIntentActivities() returns a static list of activities; this list is in your process, its contents don't change behind your back because the installed applications have changed since you retrieved the list.

To handle the set of activities changing, you need to watch for PackageManager broadcasts about applications being installed/removed (or external storage being mounted/unmounted, etc) and update your list at that point.

Here are the broadcasts to look for:

http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED

http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_CHANGED

http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REMOVED

http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED

When an update to an application is installed, you will get a series of broadcasts, so production code should be a little smart about when it updates the list.

Now that you know the list has possibly changed, you need to run your same code to re-requery the package manager, and put this new list in the adapter. Be sure to call BaseAdapter.notifyDataSetChanged() to tell the list view about the change when you do so.

Here is the code in the Settings app that is used to implement the Manage Applications UI that shows the list of installed apps. This is admittedly complicated because it is doing a lot of things -- making sure all the work it does is off the main thread (to avoid UI glitching), perform queries for various app data in the background while the user is interacting with the UI, etc. It makes use of some private APIs in the system for computing app storage sizes that you wouldn't be able to use in a third party app, but the code of it is all just regular SDK code.

https://android.googlesource.com/platform/packages/apps/Settings/+/gingerbread-release/src/com/android/settings/applications/ApplicationsState.java

0

精彩评论

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