卸载应用程序前询问密码 [英] Ask for password before uninstalling application

查看:20
本文介绍了卸载应用程序前询问密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对我的问题进行了大量研究,但找不到合适的解决方案,所以我在这里发布了我的查询.希望得到更好的解决方案:

First of all, I have researched a lot about my issue, but I could not find a proper solution so I am posting my query here. Hope to get a better solution to the issue:

我有一个要求,在用户从设置或任何其他应用程序(如 MyAppSharer)中删除我的应用程序之前,我需要向用户询问密码.我找到了一种解决方案,当用户单击卸载"按钮时,我可以成功调用我的活动.我在这里应用了技巧,并调用了服务.在服务中,我运行计时器,它每 1 秒运行一次,并在那一秒内检查正在运行的任务的最重要活动.这按预期完美运行.

I have a requirement where I need to ask for password to the user before user deletes my app from settings or from any other application like MyAppSharer. I have found one solution where I can successfully be able to call my activity when user clicks on Uninstall button. I have applied trick here, and calling service. In service, I run timer which runs every 1 second and in that one second, it checks for top most activity of running task. This is running perfectly as per expected.

现在,我的问题是,此活动出现在每个尝试卸载的应用程序用户身上.我需要我调用的活动应该只在用户尝试卸载我的应用程序时出现在我的应用程序中.

Now, my issue is, this activity apppears on each of application user tries to uninstall. I need that the activity which I call, should only appear for my application when user tries to uninstall my application.

这是我的代码:

public static final String PACKAGE_INSTALLER = "com.android.packageinstaller";
public static final String PACKAGE_INSTALLER_UNINSTALL_ACTIVITY = "com.android.packageinstaller.UninstallerActivity";


alarmTimer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);;
            ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;

            final String packageName = topActivity.getPackageName();
            String className = topActivity.getClassName();

            Log.v(TAG, "packageName:" + packageName);
            Log.v(TAG, "className:" + className);

            if (PACKAGE_INSTALLER.equals(packageName)
                && PACKAGE_INSTALLER_UNINSTALL_ACTIVITY.equals(className)) {

                //Here I need to apply one condition where package name received to be matched with my package name. But I am not sure how to fetch package name of selected application for uninstalling 
                //To Cancel Existing UninstallerActivity and redirect user to home.
                Intent homeIntent = new Intent();
                homeIntent.setAction(Intent.ACTION_MAIN);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                startActivity(homeIntent);

                //To open my activity
                    Intent loginActivity = new Intent(UninstallService.this, Act_Login.class);
                    loginActivity.putExtra(Constants.KEY_IS_FROM_SERVICE, true);
                    loginActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(loginActivity); 

            }
        }
    }, 0, 1000);

推荐答案

您应该尝试以下操作:

1st - 在 Manifest 文件中声明您的广播接收器,它将收听 QUERY_PACKAGE_RESTART :

1st - declare your broadcast recevier in the Manifest file , that will listen to QUERY_PACKAGE_RESTART :

  <receiver android:name=".UninstallReceiver">
          <intent-filter android:priority="999999">
                <action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
                <data android:scheme="package" />
          </intent-filter>
     </receiver>

2nd - 你的 UnunstallIntentReceiver java 类如下:

2nd - your UnunstallIntentReceiver java class like the following :

public class UninstallReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // fetching package names from extras
        String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES"); 

        if(packageNames!=null){
            for(String packageName: packageNames){
                if(packageName!=null && packageName.equals("application_package")){
                   // start your activity here and ask the user for the password 
                }
            }
        }
    }

    }

请给我一些反馈

希望有帮助.

这篇关于卸载应用程序前询问密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆