【发布时间】: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