Android apk安装的几种方法(4)

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);
     }
     }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值