应用在启动时刷新 Android

Apps refresh on launch Android

我已经构建了 Android 启动器并显示了已安装应用程序的列表,并且可以通过长按来删除,但是在我删除并应用后,图标仍留在抽屉中。

关于删除发生后如何刷新列表的任何想法?

谢谢

编辑 1 - 这是我删除应用程序的代码

 @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);
        }
    }
}

为我的应用完成相同的任务,

首先你需要创建extends BroadcastReceiver class并在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>

以上代码将在应用安装和卸载事件发生时触发,届时您将调用 asyntask 并更新您的适配器列表。

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.
    }
}