android - 在android中制作“卸载应用程序”按钮?
问题描述
我正在使用 recyclerView 中的应用程序抽屉制作启动器应用程序,显示设备上当前安装的每个应用程序。长按视图中的应用程序会显示一个上下文菜单,用户可以从中卸载应用程序。
不过,我根本不知道该怎么做。我希望能够 像这样提示系统“您要卸载此应用程序”对话框。
目前我有一个空的 void 方法,它将 recyclerview 中的应用程序位置作为输入参数,仅此而已。
以下是相关的 recyclerview 方法 - 如果您想要整个课程,我可以对其进行编辑。
public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {
public List<AppObject> appsList;
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.appCategoryTextView);
appDrawerItemLL = (LinearLayout) itemView.findViewById(R.id.app_drawer_item);
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}
@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 uninstallApp(int position) {
appsList.remove(position); //removes item from listview but it doesn't uninstall it
notifyDataSetChanged();
}
}
这是我的应用抽屉类中的上下文菜单方法。
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case 1: // add to favourites in homescreen
addAppToFavourites();
return true;
case 2: // show information about app
showApplicationInfo();
return true;
case 3: // uninstall application
adapter.uninstallApp(item.getGroupId());
displayMessage("Uninstalled application");
return true;
default:
displayMessage("You should not be seeing this message");
}
return super.onContextItemSelected(item);
}
如您所见,上下文菜单是在recyclerview 适配器类中创建的,然后App Drawer 类中的onContextItemSelected 方法会选择单击每个选项时发生的情况。当用户单击“卸载应用程序”时,它会运行 recyclerview 适配器类中的 .uninstallApp 方法。这是卸载功能应该去的地方。我该如何实施?
解决方案
您需要知道要卸载的应用程序的包名称。然后调用这个简单的方法-
public void uninstallApp(String packagename){
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:"+packagename));
startActivity(intent);
}
不要忘记添加此权限-
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
这应该会提示用户卸载该应用程序。
推荐阅读
- python - pip 从私有仓库安装一个包,但在代理后面安装来自 PyPi 的依赖项
- python - 如何在python中加密JSON,使用密码学出错
- c# - 如何一次在验证列表中添加值?
- ios-app-group - 在 WidgetKit 的应用程序组上存储一个数组
- laravel - Laravel Vue js 编辑模式:将值从字段传递到变量
- sql - 如何在使用 Union 时模拟 ON 条件?
- asp.net-mvc - How to fix RazorEngine issue with Html.HiddenFor
- flutter - 转换后的 JSON 在 listView 中不显示任何内容
- gitlab - Gitlab CI SAST 在后续阶段访问 gl-sast-report.json 工件
- linux - 仅将当前目录中的文件添加到 .7z 文件中,而不使用递归目录