如何检查用户在 android 中使用 intent 创建的卸载对话框中单击了什么?
How to check what the user clicked on uninstall dialog box created using intent in android?
我正在尝试创建一个能够卸载其他已安装应用程序的应用程序。因此,当我使用卸载意图时,打开的对话框要求确定或取消。那么我如何才能知道用户是选择了确定还是取消,因为我需要隐藏按下的卸载按钮。分配为代码末尾 public class viewholder 中的按钮
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
MainActivity mainActivity=new MainActivity();
private Context mContext;
List<Drawable> images;
List<String> titles;
List<String> apps;
LayoutInflater inflater;
public void uninstallAPK(String apkPackageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + apkPackageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
mContext.startActivity(intent);
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
launchBrowser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchBrowser.putExtra(Intent.EXTRA_RETURN_RESULT, true);
mContext.startActivity(launchBrowser);
mainActivity.finish();
}
public Adapter(Context ctx, List<String> titles, List<Drawable>
images,List<String> apps){
this.titles =titles;
this.images = images;
this.apps = apps;
this.inflater =LayoutInflater.from(ctx);
mContext = ctx;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view = inflater.inflate(R.layout.custom_grid_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int
position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageDrawable(images.get(position));
}
@Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView gridIcon;
TextView title;
Button button;
Button button3;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView);
gridIcon = itemView.findViewById(R.id.imageView2);
button = itemView.findViewById(R.id.button);
button3 = itemView.findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uninstallAPK(apps.get(getAdapterPosition()));
//want to hide the button if ok is pressed otherwise want
to show the button
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ParseQuery<ParseObject> linkwa = new
ParseQuery<ParseObject>("package");
linkwa.whereEqualTo("packid",
apps.get(getAdapterPosition()));
linkwa.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects,
ParseException e) {
if(e==null && objects.size()>0){
for (ParseObject object:objects){
goToUrl(object.getString("link"));
}
}
}
});
}
});
}
}
}
真的不知道你的实现情况如何,但理想情况下我会有两个按钮。一个用于取消,另一个用于卸载。而且我会在两个按钮上设置 clicklistener。因此,无论何时执行 onclickListener,都意味着用户按下了对话框的按钮。所以,第一件事,我会关闭对话框,第二件事基于用户点击的按钮
public void onClick(View v){
if(v.getId == R.id.btn_dialog_uninstall){
uninstallPackage("package_name");
dialog.dissmiss();
}else{
dialog.dissmiss(); // user pressed cancelled;
}
希望对您有所帮助。
实际上有一个解决方法,您可以尝试一下
StartActivityFotResult 这将确保您将在 onActivityResult
中获得回调
public void uninstallAPK(String apkPackageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + apkPackageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
mContext.startActivityForResult(intent, 12345);
}
现在可以查看Result-Code是否成功RESULT_OK
或RESULT_CANCELLED
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1234) {
if(resultCode == Activity.RESULT_OK){
//Deleted
}
if (resultCode == Activity.RESULT_CANCELED) {
//Dismissed
}
}
}//onActivityResult
2.1。如果上述解决方案不起作用则
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1234) {
if (appInstalledOrNot("required_app_package_name")){
// Dismissed
}else {
//Deleted
}
}
}//onActivityResult
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
PS。检查应用程序是否已安装取自 here
我正在尝试创建一个能够卸载其他已安装应用程序的应用程序。因此,当我使用卸载意图时,打开的对话框要求确定或取消。那么我如何才能知道用户是选择了确定还是取消,因为我需要隐藏按下的卸载按钮。分配为代码末尾 public class viewholder 中的按钮
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
MainActivity mainActivity=new MainActivity();
private Context mContext;
List<Drawable> images;
List<String> titles;
List<String> apps;
LayoutInflater inflater;
public void uninstallAPK(String apkPackageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + apkPackageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
mContext.startActivity(intent);
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
launchBrowser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchBrowser.putExtra(Intent.EXTRA_RETURN_RESULT, true);
mContext.startActivity(launchBrowser);
mainActivity.finish();
}
public Adapter(Context ctx, List<String> titles, List<Drawable>
images,List<String> apps){
this.titles =titles;
this.images = images;
this.apps = apps;
this.inflater =LayoutInflater.from(ctx);
mContext = ctx;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view = inflater.inflate(R.layout.custom_grid_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int
position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageDrawable(images.get(position));
}
@Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView gridIcon;
TextView title;
Button button;
Button button3;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView);
gridIcon = itemView.findViewById(R.id.imageView2);
button = itemView.findViewById(R.id.button);
button3 = itemView.findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uninstallAPK(apps.get(getAdapterPosition()));
//want to hide the button if ok is pressed otherwise want
to show the button
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ParseQuery<ParseObject> linkwa = new
ParseQuery<ParseObject>("package");
linkwa.whereEqualTo("packid",
apps.get(getAdapterPosition()));
linkwa.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects,
ParseException e) {
if(e==null && objects.size()>0){
for (ParseObject object:objects){
goToUrl(object.getString("link"));
}
}
}
});
}
});
}
}
}
真的不知道你的实现情况如何,但理想情况下我会有两个按钮。一个用于取消,另一个用于卸载。而且我会在两个按钮上设置 clicklistener。因此,无论何时执行 onclickListener,都意味着用户按下了对话框的按钮。所以,第一件事,我会关闭对话框,第二件事基于用户点击的按钮
public void onClick(View v){
if(v.getId == R.id.btn_dialog_uninstall){
uninstallPackage("package_name");
dialog.dissmiss();
}else{
dialog.dissmiss(); // user pressed cancelled;
}
希望对您有所帮助。
实际上有一个解决方法,您可以尝试一下
StartActivityFotResult 这将确保您将在 onActivityResult
中获得回调public void uninstallAPK(String apkPackageName) { Intent intent = new Intent(Intent.ACTION_DELETE); intent.setData(Uri.parse("package:" + apkPackageName)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Intent.EXTRA_RETURN_RESULT, true); mContext.startActivityForResult(intent, 12345); }
现在可以查看Result-Code是否成功
RESULT_OK
或RESULT_CANCELLED
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1234) { if(resultCode == Activity.RESULT_OK){ //Deleted } if (resultCode == Activity.RESULT_CANCELED) { //Dismissed } } }//onActivityResult
2.1。如果上述解决方案不起作用则
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1234) {
if (appInstalledOrNot("required_app_package_name")){
// Dismissed
}else {
//Deleted
}
}
}//onActivityResult
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
PS。检查应用程序是否已安装取自 here