在安卓启动器应用程序中实现"Unintstall app"和"App info"按钮



正如标题所说,我正在开发一个android应用程序。我有一个呈现我的应用程序列表的回收视图和一个带有选项"的上下文菜单;Unintstall应用程序"以及";应用程序信息";。应用程序信息按钮应该只打开应用程序本身的android设置页面,用户可以在那里卸载、清除缓存、更改权限等。

我该怎么做?它是通过软件包管理器中的某种方法实现的吗?我需要任何特殊权限吗?

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

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

这是回收站视图中的相关代码:

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

您在清单中需要此权限:

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

最新更新