【问题标题】:ask password on uninstalling android app?在卸载Android应用程序时询问密码?
【发布时间】:2015-06-15 15:16:02
【问题描述】:

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

这是我写的:

Android 清单

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

【问题讨论】:

    标签: android security


    【解决方案1】:

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

    这是不可能的。您的应用不会收到通知,也不会要求您这样做。

    【讨论】:

    【解决方案2】:

    工作原理

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

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

    【讨论】:

    • 好的,我会在我这边再试一次。
    猜你喜欢
    • 2014-03-12
    • 1970-01-01
    • 2015-06-07
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2013-09-25
    • 2021-08-18
    相关资源
    最近更新 更多