目录[-]
1.使用linux命令安装:
apk事先保存在sdcard上面,如果安装不成功,可能权限不够,chmod 777一下,问题就解决了。
1
2
3
4
5
6
7
|
try
{
String cmd =
"pm install -r sdcard/TestStartActivity_sig2.apk"
;
Runtime.getRuntime().exec(cmd);
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|
2.使用adb命令安装。
am start -n 包名/Activity所在包名及类名
adb shell am start -n com.liusl.test/com.liusl.test.MainActivity
3.使用发送Intent消息打开apk安装程序
1
2
3
4
5
6
7
8
9
10
|
public
void
installApk(String saveFileName) {
File apkfile =
new
File(saveFileName);
if
(!apkfile.exists()) {
Log.i(TAG,
"installApk..file not exist!"
+ apkfile);
return
;
}
Intent i =
new
Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse(
"file://"
+ apkfile.toString()),
"application/vnd.android.package-archive"
);
mContext.startActivity(i);
}
|
4.静默安装:安装过程中没有任何提示的安装
[1] 重写pacakgemanager的相关类
IPackageDateObserver.java ,IPackageDeleteObserver.java,IPackageInstallObserver.java,IPackageMoveObserver.java,IPackageStateObserver.java,PackageManager.java
[2] 注册消息接收类
1
2
3
4
5
6
7
8
9
10
11
|
private
static
BroadcastReceiver mPackageInstallReceiver =
new
BroadcastReceiver() {
@Override
public
void
onReceive(Context context, Intent intent) {
String action = intent.getAction();
if
(action.equals(UpdateManager.PACKAGE_INSTALL_SUCC)) {
Log.i(TAG,
" receive .PACKAGE_INSTALL_SUCC"
);
}
else
{
Log.i(TAG,
" receive .PACKAGE_INSTALL_FAILED."
);
}
}
};
|
[3] 安装apk发送消息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
private
boolean
installApkDefault(Context context, String fileName, String packageName) {
Log.d(TAG,
"installApkDefault:"
+ packageName +
",fileName:"
+ fileName);
File file =
new
File(fileName);
int
installFlags =
0
;
if
(!file.exists())
return
false
;
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
PackageManager pm = context.getPackageManager();
try
{
IPackageInstallObserver observer =
new
MyPakcageInstallObserver(context, fileName,packageName);
Log.i(TAG,
"installFlags:"
+ installFlags +
"packagename:"
+ packageName);
pm.installPackage(Uri.fromFile(file), observer, installFlags, packageName);
return
true
;
}
catch
(Exception e) {
Log.e(TAG,
"installApkDefault fail ;exception: "
+ e.getMessage());
}
return
false
;
}
private
class
MyPakcageInstallObserver
extends
IPackageInstallObserver.Stub {
Context cxt;
String filename;
String pkname;
public
MyPakcageInstallObserver(Context c, String filename, String packagename) {
this
.cxt = c;
this
.filename = filename;
this
.pkname = packagename;
}
@Override
public
void
packageInstalled(String packageName,
int
returnCode) {
Log.i(
"MyPakcageInstallObserver"
,
"returnCode = "
+ returnCode);
// 返回1代表安装成功
if
(pkname !=
null
&& pkname.equals(packageName)) {
Intent it =
new
Intent();
it.putExtra(
"install_returnCode"
, returnCode);
it.putExtra(
"install_packageName"
, packageName);
if
(returnCode ==
1
) {
it.setAction(UpdateManager.PACKAGE_INSTALL_SUCC);
}
else
{
it.setAction(UpdateManager.PACKAGE_INSTALL_FAILED);
File f =
new
File(filename);
if
(f.exists()) {
f.delete();
}
}
cxt.sendBroadcast(it);
}
}
}
|