卸载指定包名的应用,当版本更新时,如果包名改变,提醒用户删除老的版本。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String packageName = "com.example.testuninstall";
Uninstall(packageName);
}
/**
* 卸载指定包名的应用
*
* @param packageName
*/
private void Uninstall(String packageName) {
if (checkApplication(packageName)) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(packageURI);
startActivity(intent);
}
}
/**
* 判断该包名的应用是否安装
*
* @param packageName
* @return
*/
public boolean checkApplication(String packageName) {
if (packageName == null || "".equals(packageName)) {
return false;
}
try {
getPackageManager().getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
}