zoukankan      html  css  js  c++  java
  • (转)Android 应用的安装和卸载

    1.普通的应用安装和卸载

    [java] view plain copy
     
    1. /* 安装apk */    
    2. public static void installApk(Context context, String fileName) {    
    3.     Intent intent = new Intent();    
    4.     intent.setAction(Intent.ACTION_VIEW);    
    5.     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
    6.     intent.setDataAndType(Uri.parse("file://" + fileName),"application/vnd.android.package-archive");    
    7.     context.startActivity(intent);    
    8. }    
    9.     
    10. /* 卸载apk */    
    11. public static void uninstallApk(Context context, String packageName) {    
    12.     Uri uri = Uri.parse("package:" + packageName);    
    13.     Intent intent = new Intent(Intent.ACTION_DELETE, uri);    
    14.     context.startActivity(intent);    
    15. }  
    当应用安装或卸载成功后,系统会发送相应的广播

    监听系统发出的安装广播

    在安装和卸载完后,android系统会发一个广播

    android.intent.action.PACKAGE_ADDED(安装)

    android.intent.action.PACKAGE_REMOVED(卸载)


    我们可以监听这两个广播,来进行相应的处理
    [java] view plain copy
     
    1. public class MonitorSysReceiver extends BroadcastReceiver{    
    2.           
    3.     @Override      
    4.     public void onReceive(Context context, Intent intent){    
    5.         //接收安装广播     
    6.         if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {       
    7.             //TODO        
    8.         }       
    9.         //接收卸载广播      
    10.         if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {       
    11.             //TODO    
    12.         }    
    13.     }    
    14. }  
    在AndroidMainFest.xml中,我们需要注册该广播
    [java] view plain copy
     
    1. <receiver android:name=".MonitorSysReceiver">       
    2.           <intent-filter>      
    3.            <action android:name="android.intent.action.PACKAGE_ADDED" />      
    4.            <action android:name="android.intent.action.PACKAGE_REMOVED" />      
    5.           </intent-filter>      
    6. </receiver>   


    2.Android的静默安装和卸载

    Android的静默安装和卸载,需要将应用注册成系统用户组,并且需要在系统源码下编译才行。

    [html] view plain copy
     
    1. android:sharedUserId="android.uid.system"    
    还需要相应的权限
    [html] view plain copy
     
    1. <uses-permission android:name="android.permission.INSTALL_PACKAGES" />    
    2. <uses-permission android:name="android.permission.DELETE_PACKAGES" />   
    报红的话,clean一下就行了

    静默安装

    安装方法:

    [java] view plain copy
     
    1. String fileName = Environment.getExternalStorageDirectory() + File.separator + "baidu"+File.separator +"UC.apk";  
    2. Uri uri = Uri.fromFile(new File(fileName));  
    3. int installFlags = 0;  
    4. PackageManager pm = getPackageManager();  
    5. try {  
    6.     PackageInfo pi = pm.getPackageInfo("com.UCMobile",PackageManager.GET_UNINSTALLED_PACKAGES);  
    7.     if(pi != null) {  
    8.         installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;  
    9.     }  
    10. catch (Exception e) {  
    11.     Log.e("exception",e.toString());  
    12. }  
    13. MyPakcageInstallObserver observer = new MyPakcageInstallObserver();  
    14. pm.installPackage(uri, observer, installFlags, "com.UCMobile");  

    安装回调

    [java] view plain copy
     
    1. /*静默安装回调*/    
    2. class MyPakcageInstallObserver extends IPackageInstallObserver.Stub{    
    3.             
    4.     @Override    
    5.     public void packageInstalled(String packageName, int returnCode) {    
    6.         if (returnCode == 1) {    
    7.             Log.e("DEMO","安装成功");    
    8.             new ToastThread(InstallActivity.this,"安装成功").start();    
    9.         }else{    
    10.             Log.e("DEMO","安装失败,返回码是:"+returnCode);    
    11.             new ToastThread(InstallActivity.this,"安装失败,返回码是:"+returnCode).start();    
    12.         }    
    13.     }    
    14. }   


    静默卸载:

    [java] view plain copy
     
    1. PackageManager pm = ParentManagementSettings.this.getPackageManager();    
    2. IPackageDeleteObserver observer = new MyPackageDeleteObserver();    
    3. pm.deletePackage("com.readboy.microclassphone", observer, 0);  
    [html] view plain copy
     
    1. /**  
    2.  * 应用静默删除回调  
    3.  * @author 1  
    4.  *  
    5.  */  
    6. class MyPackageDeleteObserver extends IPackageDeleteObserver.Stub {    
    7.     @Override    
    8.     public void packageDeleted(String packageName, int returnCode) {    
    9.         if (returnCode == 1) {    
    10.             Log.e("DEMO","success...");  
    11.         }else{    
    12.             Log.e("DEMO","fail code:"+returnCode);     
    13.         }    
    14.     }    
    15. }   


    备注:

      根据APK的path路径,查看APK的包名

    [java] view plain copy
     
    1. PackageManager pm2=getActivity().getPackageManager();  
    2. PackageInfo info=pm2.getPackageArchiveInfo(files[i].getPath(), 0);  
    3. Log.e("DEMO", "infoName"+info.packageName);  
    4. packageNameString=info.packageName;  


     原文地址:https://blog.csdn.net/qwe380948730/article/details/54909308

     
  • 相关阅读:
    try-catch- finally块中, finally块唯一不执行的情况是什么?
    Mac下 pygame.image.load不显示图片解决方法
    Ubuntu中sudo速度慢的解决方法(转载)
    [Mac Terminal] ___中断跳出死循环
    Fuzzy logic
    Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.4 Predicates and Quantifiers
    Bugs -----The largest and most interesting kind of thing.
    A Brief, Incomplete, and Mostly Wrong History of Programming Languages
    MIPS指令集
    >> 计算机的数据表示
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/9121358.html
Copyright © 2011-2022 走看看