Ask for password before uninstalling application

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

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:

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.

Here is my code:

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

回答1:

you should try something like the following :

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

and please give me some feedback

Hope That Helps.



回答2:

If this is a corporate requirement (if you want to block a regular user from uninstalling your app, no chance, thanks Google for protecting us from bad devs), you should create a device administrator application. This way, although the user still can delete the app, it's one extra step if you want to prevent accidental erasing.

Before deleting your app, if it's enabled as device admin, the user must first disable the app as administrator, and the app receives this broadcast.

In your XML, put

<activity android:name=".app.DeviceAdminSample"             android:label        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!