Issue
I'm making a launcher application with an app drawer in a recyclerView, showing every currently installed app on the device. Long pressing the apps in the view shows a context menu, from which the user can uninstall an application.
I simply have no clue how I'd go about doing that though. I want to be able to just prompt the system "Do you want to uninstall this app" dialog like this.
Currently I've got an empty void method which takes the application position in the recyclerview as an input parameter and nothing more.
Here are the relevant recyclerview methods - If you want the entire class, I can edit it in.
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();
}
}
And here is the context menu method in my app drawer class.
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);
}
As you can see, the context menu is created in the recyclerview adapter class, then the onContextItemSelected method in the App Drawer class chooses what happens with each option clicked. When the user clicks "Uninstall app", it runs the .uninstallApp method in the recyclerview adapter class. This is where the uninstallation function is supposed to go. How can I implement that?
Solution
You need to know the package name of the app you're trying to uninstall. then call this simple method-
public void uninstallApp(String packagename){
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:"+packagename));
startActivity(intent);
}
Don't forget to add this permission-
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
This should prompt the user to uninstall that app.
Answered By - LegendSayantan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.