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

在卸载android应用程序时询问密码?,android,security,Android,Security,我正在开发一个应用程序,它可以存储有关通话和消息的信息。我不希望用户在不输入密码的情况下卸载应用程序。我想阻止用户这样做。我也查看了这些链接,但我找不到任何线索: 以下是我写的: 安卓清单 <receiver android:name=".DetectRemoved" > <intent-filter android:priority="999999"> <action android:nam

我正在开发一个应用程序,它可以存储有关通话和消息的信息。我不希望用户在不输入密码的情况下卸载应用程序。我想阻止用户这样做。我也查看了这些链接,但我找不到任何线索:

以下是我写的:

安卓清单

<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>
我不希望用户在不输入密码的情况下卸载应用程序


这是不可能的。不会通知您的应用程序,也不会请求您的许可。

它是如何工作的

在manifest.xml中

添加权限:

<uses-permission android:name="android.permission.GET_TASKS"/>
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();
}
}
类ListenActivities扩展线程{
布尔退出=假;
ActivityManager am=null;
Context=null;
公共列表活动(上下文con){
上下文=con;
am=(ActivityManager)context.getSystemService(context.ACTIVITY\u服务);
}
公开募捐{
Looper.prepare();
当(!退出){
//从当前正在运行的任务中获取信息
列出taskInfo=am.getRunningTasks(最大优先级);
字符串activityName=taskInfo.get(0.topActivity.getClassName();
Log.d(“topActivity”,“当前活动:”
+活动名称);
if(activityName.equals(“com.android.packageinstaller.UninstallerActivity”)){
//用户已单击“管理应用程序设置”下的“卸载”按钮
//执行您想在此处执行的任何预卸载任务
//显示对话或启动其他活动或数据库操作等。。
//context.startActivity(新意图(context,MyPreUninstallationMsgActivity.class).setFlags(意图.FLAG_活动_新任务));
退出=真;
Toast.makeText(上下文,“完成预卸载任务…立即退出”,Toast.LENGTH_SHORT.show();
}else if(activityName.equals(“com.android.settings.ManageApplications”)){
//按下“后退”按钮,用户已返回到“管理应用程序”窗口
//我们现在应该关闭活动监视
退出=真;
}
}
loop.loop();
}
}

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

谢谢,但如何检测我的应用被删除或卸载?可能?删除包后会广播删除消息,因此其他人会收到通知,因为您的包当时已被删除,所以“否”。请参见broadasts
PACKAGE.*
此处:好的,我将从myside重试。
<receiver android:name=".UninstallIntentReceiver">
  <intent-filter android:priority="0">
        <action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
        <data android:scheme="package" />
  </intent-filter>
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();

            }
        }
    }
}

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