android 静默安装,含获取各种应用信息方法,根据apk获取应用信息
16lz
2021-01-24
抄来好文章,哈哈
通常情况下,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 | import java.io.File; |
004 |
005 | import java.io.FileNotFoundException; |
006 |
007 | import java.io.FileOutputStream; |
008 |
009 | import java.io.IOException; |
010 |
011 | import android.content.Context; |
012 |
013 | import android.content.Intent; |
014 |
015 | import android.content.pm.PackageInfo; |
016 |
017 | import android.content.pm.PackageManager; |
018 |
019 | import android.content.pm.PackageManager.NameNotFoundException; |
020 |
021 | import android.content.pm.ApplicationInfo; |
022 |
023 | import android.content.pm.PackageParser; |
024 |
025 | import android.net.Uri; |
026 |
027 | import android.util.Log; |
028 |
029 | import android.util.DisplayMetrics; |
030 |
031 | import android.content.pm.IPackageInstallObserver; |
032 |
033 | import android.content.pm.IPackageDeleteObserver; |
034 |
035 | import android.os.FileUtils; |
036 |
037 | import android.os.Handler; |
038 |
039 | import android.os.Message; |
040 |
041 |
|
042 |
043 | public class PackageInstaller { |
044 |
045 |
|
046 |
047 | private File 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 static String TAG = "PackInstaller" ; |
056 |
057 | private Context mContext; |
058 |
059 | private ApplicationInfo mAppInfo; |
060 |
061 | public PackageInstaller(Context context) { |
062 |
063 | mContext = context; |
064 |
065 | } |
066 |
067 | public void install(String path,String packageName){ |
068 |
069 | Intent intent = new Intent(Intent.ACTION_VIEW); |
070 |
071 | intent.setDataAndType(Uri.fromFile( new File(path)), |
072 |
073 | "application/vnd.android.package-archive" ); |
074 |
075 | mContext.startActivity(intent); |
076 |
077 | } |
078 |
079 |
|
080 |
081 | public void instatllBatch(String path) { |
082 |
083 | Log.i(TAG, "path=" + path); |
084 |
085 | int installFlags = 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 = new PackageInstallObserver(); |
124 |
125 | pm.installPackage(mPackageURI, observer, installFlags, |
126 |
127 | packageName); |
128 |
129 | } |
130 |
131 | private class PackageInstallObserver extends IPackageInstallObserver.Stub { |
132 |
133 | public void packageInstalled(String packageName, int returnCode) { |
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 extends IPackageDeleteObserver.Stub { |
148 |
149 | public void packageDeleted( boolean succeeded) { |
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 void uninstall(String packageName){ |
164 |
165 | Uri packageURI = Uri.parse( "package:" + packageName); |
166 |
167 | Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, |
168 |
169 | packageURI); |
170 |
171 | mContext.startActivity(uninstallIntent); |
172 |
173 | } |
174 |
175 |
|
176 |
177 | public void uninstallBatch(String packageName) { |
178 |
179 | PackageDeleteObserver observer = new PackageDeleteObserver(); |
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(安卓)装载器---在应用程序中使用装载器
- JS与Android互调
- Android支付——支付宝支付总结
- 【Android】19.1 SharedPreferences类
- Android小知识点收集
- 认识Activity,创建第一个android应用
- Android账户同步备份机制
- Android应用程序组件Content Provider简要介绍和学习计划
- Android(Xamarin)之旅(三)