在卸载Android应用程序时询问密码?

时间:2015-06-15 06:35:44

标签: android security

我正在开发一个应用程序,用于存储有关呼叫和消息的信息。我不希望用户在没有输入密码的情况下卸载应用程序。我想阻止用户这样做。我也研究了这些链接,但我无法得到线索: Ask for password before uninstalling application

这是我写的:

Android Manifest

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

Java代码

 public class DetectRemoved extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");

            if(packageNames!=null){
                for(String packageName: packageNames){
                    if(packageName!=null && packageName.equals("activity_log.pargansystem.com.activity_log")){
                        Toast.makeText(context, "your message", Toast.LENGTH_SHORT).show();
                        // start your activity here and ask the user for the password
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

  

我不希望用户在不输入密码的情况下卸载应用程序

这是不可能的。您的应用将不会收到通知,也不会被要求获得许可。

答案 1 :(得分:1)

工作原理

在manifest.xml中

添加权限:

<uses-permission android:name="android.permission.GET_TASKS"/>

和广播接收者:

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

UninstallIntentReceiver.java(广播接收器类)

public class UninstallIntentReceiver 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("YOUR_APPLICATION_PACKAGE_NAME")){
                // User has selected our application under the Manage Apps settings
                // now initiating background thread to watch for activity
                new ListenActivities(context).start();

            }
        }
    }
}

}

ListenActivities类 - 用于监视前台活动

class ListenActivities extends Thread{
boolean exit = false;
ActivityManager am = null;
Context context = null;

public ListenActivities(Context con){
    context = con;
    am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

public void run(){

    Looper.prepare();

    while(!exit){

         // get the info from the currently running task
         List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(MAX_PRIORITY); 

         String activityName = taskInfo.get(0).topActivity.getClassName();


         Log.d("topActivity", "CURRENT Activity ::"
                 + activityName);

         if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
            // User has clicked on the Uninstall button under the Manage Apps settings

             //do whatever pre-uninstallation task you want to perform here
             // show dialogue or start another activity or database operations etc..etc..

            // context.startActivity(new Intent(context, MyPreUninstallationMsgActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
             exit = true;
             Toast.makeText(context, "Done with preuninstallation tasks... Exiting Now", Toast.LENGTH_SHORT).show();
        } else if(activityName.equals("com.android.settings.ManageApplications")) {
            // back button was pressed and the user has been taken back to Manage Applications window
                      // we should close the activity monitoring now
            exit=true;
        }
    }
    Looper.loop();
}
}

这是我在某些链接中找到的代码希望这对您有所帮助。