Search code examples
javaandroidlauncher

Apps refresh on launch Android


I've built and Android launcher and have the list of installed apps showing, and am able to delete on long press but after I delete and app the icon stays in the drawer.

Any ideas on how to have the list refresh after the deletion happens?

Thanks

Edit 1 - Heres my code for the app deletion

 @Override
public void onGridItemLongClick(GridView g, View v, int position, long id) {
    AppModel app = (AppModel) getGridAdapter().getItem(position);
    if (app != null) {
        Uri packageURI = Uri.parse("package:"+app.getApplicationPackageName());
        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);

        if (uninstallIntent != null) {
            startActivity(uninstallIntent);
        }
    }
}

Solution

  • same task done for my app,

    First you need to create extends BroadcastReceiver class and register in Manifest,

    InstallUninstallReceiver.java

      public class InstallUninstallReceiver extends BroadcastReceiver
        {
            private static final String TAG = InstallUninstallReceiver.class.getSimpleName();
    
            @Override
            public void onReceive(Context context, Intent intent)
            {
               startLoaderAsyncTask();
            }
    private LoaderAsyncTask mLoaderAsyncTask;
    private void startLoaderAsyncTask()
        {
            if(mLoaderAsyncTask != null)
            {
                mLoaderAsyncTask.cancel(true);
                mLoaderAsyncTask = null;
            }
            if(mLoaderAsyncTask == null)
                mLoaderAsyncTask = new LoaderAsyncTask();
            mLoaderAsyncTask.execute();
        }
        }
    

    AndriodManifest.xml

    <receiver android:name=".InstallUninstallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
    

    Above code will trigger when app install and uninstall event occur on that time you will call asyntask and update your adapter list.

    LoaderAsyncTask.java

    public class LoaderAsyncTask extends AsyncTask<Void, PackageInfo, Void> {
        ProgressDialog mDialog;
        private ArrayList<PackageInfo> mList = new ArrayList();
        public LoaderAsyncTask()
        {
            mDialog = ProgressDialog.show(mActivity, "Loading", "Pls wait a moment...");
            mList.clear();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            try
            {
                refreshAppList();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
    
        private void refreshAppList() {
            PackageManager pkgMgr = mActivity.getPackageManager();
            List<PackageInfo> pgs = pkgMgr.getInstalledPackages(PackageManager.GET_PERMISSIONS);
            for (int i = 0; i < pgs.size(); i++) 
            {
                        if(isCancelled())
                            break;
                        PackageInfo p = pgs.get(i);
                       mList.add(p);
            }
        }
    
    
    
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            mDialog.dismiss();
          //  mGridAdapter.setData(mList);//now you will get update app list , set to your GridAdapter refresh the Launcher.
        }
    }