android 静默安装,含获取各种应用信息方法,根据apk获取应用信息

抄来好文章,哈哈


通常情况下,android是没有提供静默方式的上层接口,我们需要在android源代码下来调用这个隐藏的接口来完成静默安装。
最重要的就是参考android系统目录下的packages/apps/PackageInstaller,
当中有两个文件 PackageInstallerActivity.java,InstallAppProgress.java ,前者就是我们通常看到的带有提示对话框的安装应用程序,后者是点确定安装后调用的intent。
现提供一个静默安装的关键类,该类在android2.2下成功编译, 其中通过循环调用接口instatllBatch则可实现批量安装
当然最后的应用程序别忘记添加权限

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2     <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
3     <uses-permission android:name="android.permission.DELETE_PACKAGES" />
4     <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
5     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
6     <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />

查看源码
打印 ?
001 package com.android.util;
002  
003 importjava.io.File; 
004  
005 importjava.io.FileNotFoundException; 
006  
007 importjava.io.FileOutputStream; 
008  
009 importjava.io.IOException; 
010  
011 importandroid.content.Context; 
012  
013 importandroid.content.Intent; 
014  
015 importandroid.content.pm.PackageInfo; 
016  
017 importandroid.content.pm.PackageManager; 
018  
019 importandroid.content.pm.PackageManager.NameNotFoundException; 
020  
021 import android.content.pm.ApplicationInfo;
022  
023 import android.content.pm.PackageParser;
024  
025 importandroid.net.Uri; 
026  
027 importandroid.util.Log; 
028  
029 import android.util.DisplayMetrics;
030  
031 importandroid.content.pm.IPackageInstallObserver; 
032  
033 importandroid.content.pm.IPackageDeleteObserver; 
034  
035 importandroid.os.FileUtils; 
036  
037 import android.os.Handler;
038  
039 import android.os.Message;
040  
041   
042  
043 public classPackageInstaller { 
044  
045   
046  
047 privateFile mTmpFile; 
048  
049 private final int INSTALL_COMPLETE = 1;
050  
051 final static int SUCCEEDED = 1;
052  
053 final static int FAILED = 0;
054  
055 private final staticString TAG = "PackInstaller"
056  
057 privateContext mContext; 
058  
059 private ApplicationInfo mAppInfo;
060  
061 publicPackageInstaller(Context context) { 
062  
063 mContext = context; 
064  
065
066  
067 public voidinstall(String path,String packageName){ 
068  
069 Intent intent = newIntent(Intent.ACTION_VIEW); 
070  
071 intent.setDataAndType(Uri.fromFile(newFile(path)), 
072  
073 "application/vnd.android.package-archive"); 
074  
075 mContext.startActivity(intent); 
076  
077
078  
079   
080  
081 public voidinstatllBatch(String path) { 
082  
083 Log.i(TAG, "path="+ path); 
084  
085 intinstallFlags = 0
086  
087 Uri mPackageURI  = Uri.fromFile(new File(path));
088  
089 PackageParser.Package mPkgInfo = getPackageInfo(mPackageURI);
090  
091 mAppInfo =  mPkgInfo.applicationInfo;
092  
093 String packageName = mAppInfo.packageName;
094  
095 Log.i(TAG, "====install packageName ="+packageName);
096  
097 PackageManager pm = mContext.getPackageManager(); 
098  
099 try
100  
101 PackageInfo pi = pm.getPackageInfo(packageName, 
102  
103 PackageManager.GET_UNINSTALLED_PACKAGES); 
104  
105 if(pi != null) { 
106  
107 installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; 
108  
109
110  
111 catch(NameNotFoundException e) { 
112  
113
114  
115 if((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { 
116  
117 Log.w(TAG, "Replacing package:"+ packageName); 
118  
119
120  
121   
122  
123 PackageInstallObserver observer = newPackageInstallObserver(); 
124  
125 pm.installPackage(mPackageURI, observer, installFlags, 
126  
127 packageName); 
128  
129
130  
131 private class PackageInstallObserver extendsIPackageInstallObserver.Stub { 
132  
133 public void packageInstalled(String packageName, intreturnCode) { 
134  
135 // Message msg = mHandler.obtainMessage(INSTALL_COMPLETE); 
136  
137 // msg.arg1 = returnCode; 
138  
139 // mHandler.sendMessage(msg); 
140  
141 Log.i(TAG, "====INSTALL_COMPLETE"); 
142  
143
144  
145
146  
147 private class PackageDeleteObserver extendsIPackageDeleteObserver.Stub { 
148  
149 public void packageDeleted(booleansucceeded) { 
150  
151 //            Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE); 
152  
153 //            msg.arg1 = succeeded?SUCCEEDED:FAILED; 
154  
155 //            mHandler.sendMessage(msg); 
156  
157 Log.i(TAG, "====UNINSTALL_COMPLETE"); 
158  
159
160  
161
162  
163     public voiduninstall(String packageName){ 
164  
165 Uri packageURI = Uri.parse("package:"+ packageName); 
166  
167 Intent uninstallIntent = newIntent(Intent.ACTION_DELETE, 
168  
169 packageURI); 
170  
171 mContext.startActivity(uninstallIntent); 
172  
173
174  
175   
176  
177 public voiduninstallBatch(String packageName) { 
178  
179 PackageDeleteObserver observer = newPackageDeleteObserver(); 
180  
181 mContext.getPackageManager().deletePackage(packageName, observer, 0); 
182  
183   
184  
185
186  
187 /* 
188  
189 *  Utility method to get package information for a given packageURI          
190  
191 */
192  
193 public  PackageParser.Package getPackageInfo(Uri packageURI) {
194  
195 final String archiveFilePath = packageURI.getPath();
196  
197 PackageParser packageParser = new PackageParser(archiveFilePath);
198  
199 File sourceFile = new File(archiveFilePath);
200  
201 DisplayMetrics metrics = new DisplayMetrics();
202  
203 metrics.setToDefaults();
204  
205 PackageParser.Package pkg =  packageParser.parsePackage(sourceFile,
206  
207 archiveFilePath, metrics, 0);
208  
209 // Nuke the parser reference.
210  
211 packageParser = null;
212  
213 return pkg;
214  
215 }
216  
217 /*
218  
219 * Utility method to get application information for a given packageURI
220  
221 */
222  
223 public   ApplicationInfo getApplicationInfo(Uri packageURI) {
224  
225 final String archiveFilePath = packageURI.getPath();
226  
227 PackageParser packageParser = new PackageParser(archiveFilePath);
228  
229 File sourceFile = new File(archiveFilePath);

你可能感兴趣的:(android,packageinfo,ApplicationInfo,PackageParser)