1. 程式人生 > >Android 應用的安裝和解除安裝

Android 應用的安裝和解除安裝

1.普通的應用安裝和解除安裝

/* 安裝apk */  
public static void installApk(Context context, String fileName) {  
    Intent intent = new Intent();  
    intent.setAction(Intent.ACTION_VIEW);  
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    intent.setDataAndType(Uri.parse("file://" + fileName),"application/vnd.android.package-archive");  
    context.startActivity(intent);  
}  
  
/* 解除安裝apk */  
public static void uninstallApk(Context context, String packageName) {  
    Uri uri = Uri.parse("package:" + packageName);  
    Intent intent = new Intent(Intent.ACTION_DELETE, uri);  
    context.startActivity(intent);  
}
當應用安裝或解除安裝成功後,系統會發送相應的廣播

監聽系統發出的安裝廣播

在安裝和解除安裝完後,android系統會發一個廣播

android.intent.action.PACKAGE_ADDED(安裝)

android.intent.action.PACKAGE_REMOVED(解除安裝)


我們可以監聽這兩個廣播,來進行相應的處理
public class MonitorSysReceiver extends BroadcastReceiver{  
        
    @Override    
    public void onReceive(Context context, Intent intent){  
        //接收安裝廣播   
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {     
            //TODO      
        }     
        //接收解除安裝廣播    
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {     
            //TODO  
        }  
    }  
}
在AndroidMainFest.xml中,我們需要註冊該廣播
<receiver android:name=".MonitorSysReceiver">     
          <intent-filter>    
           <action android:name="android.intent.action.PACKAGE_ADDED" />    
           <action android:name="android.intent.action.PACKAGE_REMOVED" />    
          </intent-filter>    
</receiver> 


2.Android的靜默安裝和解除安裝

Android的靜默安裝和解除安裝,需要將應用註冊成系統使用者組,並且需要在系統原始碼下編譯才行。

android:sharedUserId="android.uid.system"  
還需要相應的許可權
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />  
<uses-permission android:name="android.permission.DELETE_PACKAGES" /> 
報紅的話,clean一下就行了

靜默安裝

安裝方法:

        String fileName = Environment.getExternalStorageDirectory() + File.separator + "baidu"+File.separator +"UC.apk";
        Uri uri = Uri.fromFile(new File(fileName));
        int installFlags = 0;
        PackageManager pm = getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo("com.UCMobile",PackageManager.GET_UNINSTALLED_PACKAGES);
            if(pi != null) {
                installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
            }
        } catch (Exception e) {
            Log.e("exception",e.toString());
        }
        MyPakcageInstallObserver observer = new MyPakcageInstallObserver();
        pm.installPackage(uri, observer, installFlags, "com.UCMobile");

安裝回調

/*靜默安裝回調*/  
class MyPakcageInstallObserver extends IPackageInstallObserver.Stub{  
          
    @Override  
    public void packageInstalled(String packageName, int returnCode) {  
        if (returnCode == 1) {  
            Log.e("DEMO","安裝成功");  
            new ToastThread(InstallActivity.this,"安裝成功").start();  
        }else{  
            Log.e("DEMO","安裝失敗,返回碼是:"+returnCode);  
            new ToastThread(InstallActivity.this,"安裝失敗,返回碼是:"+returnCode).start();  
        }  
    }  
} 


靜默解除安裝:

	PackageManager pm = ParentManagementSettings.this.getPackageManager();  
	IPackageDeleteObserver observer = new MyPackageDeleteObserver();  
	pm.deletePackage("com.readboy.microclassphone", observer, 0);
	/**
	 * 應用靜默刪除回撥
	 * @author 1
	 *
	 */
	class MyPackageDeleteObserver extends IPackageDeleteObserver.Stub {  
	    @Override  
	    public void packageDeleted(String packageName, int returnCode) {  
	        if (returnCode == 1) {  
	            Log.e("DEMO","success...");
	        }else{  
	            Log.e("DEMO","fail code:"+returnCode);   
	        }  
	    }  
	} 


備註:

  根據APK的path路徑,檢視APK的包名

                    	PackageManager pm2=getActivity().getPackageManager();
                    	PackageInfo info=pm2.getPackageArchiveInfo(files[i].getPath(), 0);
                    	Log.e("DEMO", "infoName"+info.packageName);
                    	packageNameString=info.packageName;