【问题标题】:Implementing an "Unintstall app" and "App info" button in an android launcher application在 android 启动器应用程序中实现“卸载应用程序”和“应用程序信息”按钮
【发布时间】:2021-07-02 12:27:52
【问题描述】:

正如标题所说,我正在开发一个安卓应用程序。我有一个 recyclerview 可以呈现我的应用程序列表和一个带有“卸载应用程序”和“应用程序信息”选项的上下文菜单。 App Info 按钮应该只是打开应用程序本身的 android 设置页面,用户可以在其中卸载、清除缓存、更改权限等。

我该怎么做呢?是通过packagemanager中的某种方法吗?我需要任何特殊权限吗?

这是在我的应用抽屉类中处理上下文菜单选择的代码:

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId())
        {
            case 1: // add to favourites in homescreen
                displayMessage("Added to Favourites");
                return true;

            case 2: // show information about app
                return true;

            case 3: // uninstall application
                displayMessage("Uninstalled application");
                return true;

            // delete this later, it's supposed to be an unreachable message
            default:
                displayMessage("You should not be seeing this message");
        }

        return super.onContextItemSelected(item);
    }

这里是recyclerview中的相关代码:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {
        public TextView appNameTV;
        public ImageView appIconIV;
        public TextView appCategoryTV;
        public LinearLayout appDrawerItemLL;

        //This is the subclass ViewHolder which simply
        //'holds the views' for us to show on each row
        public ViewHolder(View itemView) {
            super(itemView);

            //Finds the views from our row.xml
            appNameTV = (TextView) itemView.findViewById(R.id.applicationNameTextView);
            appIconIV = (ImageView) itemView.findViewById(R.id.applicationIconImageView);
            appCategoryTV = (TextView) itemView.findViewById(R.id.appCategory);
            appDrawerItemLL = (LinearLayout) itemView.findViewById(R.id.app_drawer_item);

            itemView.setOnClickListener(this);
            itemView.setOnCreateContextMenuListener(this);
        }

        @Override
        public void onClick (View v) {
            int pos = getAdapterPosition();
            Context context = v.getContext();

            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(appsList.get(pos).getPackageName());
            context.startActivity(launchIntent);
            //Toast.makeText(v.getContext(), appsList.get(pos).getName(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.add(this.getAdapterPosition(), 1, 0, "Add to Favourites");
            menu.add(this.getAdapterPosition(), 2, 1, "App info");
            menu.add(this.getAdapterPosition(), 3, 2, "Uninstall app");
        }
    }

    public void addApp(AppObject app) {
        appsList.add(app);
    }

【问题讨论】:

    标签: android android-layout android-recyclerview android-launcher


    【解决方案1】:

    您需要在清单中获得此权限:

    <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
    

    这是卸载任何软件包的静态方法(受保护的系统或设备制造商应用程序将无法运行)。将始终提示用户同意,因此无法强制卸载。

    public static boolean deinstallApp(Context appContext,String packageName)
    {
        try {
            Uri apkUri = Uri.parse("package:" + packageName);
            Intent intent = new Intent(Intent.ACTION_DELETE, apkUri);
            appContext.startActivity(intent);
            return true;
        }catch (Exception e)
        {
            Log.e("deinstallApp"," error uninstalling. package not found?!");
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 2021-03-28
      相关资源
      最近更新 更多