password protected uninstall in android 4+ versions [programmatically]

*爱你&永不变心* 提交于 2019-12-02 10:31:48

问题


i want my app to have a password protected uninstall ... (like app lock)

im using folling code it works on Android 2.3 but not on Android 4+ versions

MANIFEST FILE

  <receiver android:name="fyp.invisibleink.UninstallIntentReceiver" >
        <intent-filter android:priority="0" >
            <action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />

            <data android:scheme="package" />
        </intent-filter>
    </receiver>

Uninstalling Activity code

ublic class Uninstalling extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_uninstalling);

    // listener
    Button settingsBtn = (Button) findViewById(R.id.btnu);
    settingsBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final EditText e1 = (EditText) findViewById(R.id.etpass);

            final String pass = e1.getText().toString();

            SharedPreferences cupSetting = getSharedPreferences(
                    "cuppassword", Context.MODE_PRIVATE);
            String mypass = cupSetting.getString("pass", "");

            if (pass.equals(mypass)) {
                Toast.makeText(getApplicationContext(),
                        "password is correct!", Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(),
                        "press OK to uninstall!", Toast.LENGTH_LONG).show();
                finish();

            } else {
                Toast.makeText(getApplicationContext(),
                        "Access Denied! try again", Toast.LENGTH_LONG)
                        .show();
                e1.setText("");

            }
        }
    });

}

UninstallReceveier

public class UninstallIntentReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    // 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("com.example.invisibleink")) {
                // 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);
    }

    @Override
    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

                Intent intent = new Intent(context, Uninstalling.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                exit = true;
                // 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));

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

}


Permissioms

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

回答1:


I am not positive, so please correct me if I'm wrong but this might have been removed as a security feature. A malicious app might be able to create a password and avoid the app from being removed creating further damage on the device. Like I said, I could be wrong but this is a guess that seems legit. I noticed that 4+ did a couple things for security because of a lot of malicious stuff and exploits happening within 3rd party apps.



来源:https://stackoverflow.com/questions/25819780/password-protected-uninstall-in-android-4-versions-programmatically

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