Android笔记--卸载

Posted ljt2724960661

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android笔记--卸载相关的知识,希望对你有一定的参考价值。

         这一节主要简单总结一下android里面常见的卸载方式,如下:

方式1:

 private void executeUnInstall(String packageName) 
        try 
            Intent intent = new Intent(this, this.getClass());
            PendingIntent sender = PendingIntent.getActivity(this, 0, intent, 0);
            PackageInstaller mPackageInstaller = getPackageManager().getPackageInstaller();
            mPackageInstaller.uninstall(packageName, sender.getIntentSender());
         catch (Exception e) 
            e.printStackTrace();
        
    

方式2:

 private void executeUnInstall(String  packageName)  
        try 
            PackageDeleteObserver deleteObserver = new PackageDeleteObserver();
            PackageManager pm = this.getPackageManager();
            Class[] unInstallTypes = new Class[] String.class, IPackageDeleteObserver.class, int.class;
            Method unInstallMethod = pm.getClass().getMethod("deletePackage", unInstallTypes);
            unInstallMethod.invoke(pm, new Object[] packageName, deleteObserver, 0);
         catch (Exception e) 
            e.printStackTrace();
        
    

    class PackageDeleteObserver extends IPackageDeleteObserver.Stub 

        public void packageDeleted(String packageName, int returnCode) throws RemoteException 
            LogUtils.i(" -- packageDeleted --->packageName:" + packageName);
        
    

    private void registerUnInstallReceiver() 
        unInstallBroadCastReceiver = new UnInstallBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
        registerReceiver(unInstallBroadCastReceiver,intentFilter);
    

    public class  UnInstallBroadCastReceiver extends BroadcastReceiver 

        @Override
        public void onReceive(Context context, Intent intent) 
            if(intent.getAction() == Intent.ACTION_PACKAGE_REMOVED) 
            
        
    
单独建立一个文件:
包名:
package android.content.pm;

public interface IPackageDeleteObserver extends android.os.IInterface 
public abstract static class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver 

public Stub() 
throw new RuntimeException("Stub!");

public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj) 

throw new RuntimeException("Stub!");


public android.os.IBinder asBinder() 

throw new RuntimeException("Stub!");



public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)

throws android.os.RemoteException 

throw new RuntimeException("Stub!");



public abstract void packageDeleted(java.lang.String packageName, int returnCode)
throws android.os.RemoteException;

方式3:

   public static void uninstallByShell(String pkgName) 
        try 
            //  adb shell pm uninstall com.xxx.xxx
            Runtime.getRuntime().exec("pm uninstall " + pkgName);
         catch (IOException e) 
            e.printStackTrace();
        
    

方式4:

 public static void uninstallByIntent(Context context, String pkgName) 
        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:" + pkgName));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
  

以上是关于Android笔记--卸载的主要内容,如果未能解决你的问题,请参考以下文章

android深度探索读书笔记

《Android深度探索》卷1 第六章笔记 1314 王宁

Linux高级-2.1软件安装与卸载-笔记

Linux学习笔记之九————ubuntu软件安装与卸载

Android深度探索(卷1)HAL与驱动开发第六章读书笔记

android 零星调试笔记