首先需要
import android.content.pm.IPackageDeleteObserver;
此class系統會callback returnCode告知app是否刪除成功
class PackageDeleteObserver extends IPackageDeleteObserver.Stub { public void packageDeleted(String packageName, int returnCode) { Log.i("service", "uninstall packageDeleted callback!!!"); if(returnCode!=1) //returnCode不等於一,代表刪除失敗 else //returnCode等於一代表刪除成功
呼叫靜默刪除function
private void uninstallapp(){ PackageDeleteObserver observer = new PackageDeleteObserver(); PackageManager pm = this.getApplicationContext().getPackageManager(); pm.deletePackage(packagename, observer, 0); }Manifest.xml要加以下權限
uses-permission android:name="android.permission.INSTALL_PACKAGES" uses-permission android:name="android.permission.DELETE_PACKAGES"
參考網頁
PackageManagerService.java source code
沒有留言:
張貼留言