2015年9月4日 星期五

Uninstall app programmatically

Code snippet:

private static final int REQUEST_UNINSTALL = 0;
public void uninstallPkg(String packageName, boolean allUsers) {
// Create new intent to launch Uninstaller activity
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, allUsers);
  uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(uninstallIntent, REQUEST_UNINSTALL);
}
 
// The result code is not Activity.RESULT_OK
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
    boolean result = false;
    if(data != null){
    result = data.getBooleanExtra(Intent.EXTRA_RETURN_RESULT, true);
    }
    if ((requestCode == REQUEST_UNINSTALL) && result) {
// TODO
  }
}

沒有留言: