当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现静默安装实例代码

Android实现静默安装实例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

静默安装主要分为以下几种方式:

一、在root过的机器上,在app中使用pm install指令安装apk:

    // 申请su权限
    process process = runtime.getruntime().exec("su");
    dataoutputstream = new dataoutputstream(process.getoutputstream());
    // 执行pm install命令
    string command = "pm install -r " + apkpath + "\n";
    dataoutputstream.write(command.getbytes(charset.forname("utf-8")));
    dataoutputstream.flush();
    dataoutputstream.writebytes("exit\n");
    dataoutputstream.flush();
    process.waitfor();
    errorstream = new bufferedreader(new inputstreamreader(process.geterrorstream()));
    string msg = "";
    string line;
    // 读取命令的执行结果
    while ((line = errorstream.readline()) != null) {
      msg += line;
    }
    log.d("tag", "install msg is " + msg);
    // 如果执行结果中包含failure字样就认为是安装失败,否则就认为安装成功
    if (!msg.contains("failure")) {
      result = true;
    }

二、修改系统应用packagemanagerintaller.apk的源码,增加无界面的安装接口:

原理

在android的文件夹中点击一个apk时,触发安装事件,packageinstaller接收系统服务packagemanagerservice传来的intent信息,传来的intent信息中有apk的一些参数。实现的关键是区分一般apk和特定apk。通过传给packagemanagerservice的intent中添加特别的参数,packageinstaller接收后进行判断,进行特别的隐藏安装流程。这个实现只能通过程序调用的方式安装。

安装过程的信息窗口在packageinstallactivity.java中实现的。其中安装过程的信息窗口有如下4个:

  1. 安装权限确认窗口:installpermissionconfirm
  2. 安装进度条:installprogress
  3. 安装结果窗口:installresult
  4. 安装错误提示对话框

需要实现一个pakkageinstallactivityhide.java的文件,去掉上面的dialog和窗口。

具体实现

1、最终安装及卸载的类如下:

installappprogress.java 
packageinstalleractivity.java
+packageinstalleractivityhide.java
packageutil.java 
uninstallappprogress.java
uninstalleractivity.java
+uninstalleractivityhide.java

2、在androidmainfest.xml声明一个特定的intent:android.intent.action.view.hide,由packageinstallactivityhide.java来接收
注意这里的两点:

  1. 把原先的 <application android:label="@string/app_name" android:theme="@android:style/theme.holo.dialogwhenlarge">改成 <application android:label="@string/app_name">,
  2. 把android:theme="@android:style/theme.holo.dialogwhenlarge"主题的显示放在每一个<activity 中,兼容正常安装的ui主题不变,隐藏安装的pakkageinstallactivityhide <activity的主题只能是:android:theme="@android:style/theme.nodisplay" 只能是这个,没有窗口

代码如下:

<application android:label="@string/app_name">
<activity android:name=".packageinstalleractivity" android:theme="@android:style/theme.holo.dialogwhenlarge" android:configchanges="orientation|keyboardhidden"> 
<intent-filter> 
<action android:name="android.intent.action.view"/> 
<category android:name="android.intent.category.default"/> 
<data android:scheme="content"/> 
<data android:scheme="file"/> 
<data android:mimetype="application/vnd.android.package-archive"/> 
</intent-filter> 
</activity> 
<activity android:name=".packageinstallerhideactivity" android:theme="@android:style/theme.nodisplay" android:configchanges="orientation|keyboardhidden"> 
<intent-filter> 
<action android:name="android.intent.action.view.hide"/> 
<category android:name="android.intent.category.default"/> 
<data android:scheme="content"/> 
<data android:scheme="file"/> 
<data android:mimetype="application/vnd.android.package-archive"/> 
</intent-filter> 
</activity> 
<activity android:name=".uninstalleractivityhide" android:theme="@android:style/theme.nodisplay" android:configchanges="orientation|keyboardhidden" android:excludefromrecents="true"> 
<intent-filter> 
<action android:name="android.intent.action.view"/> 
<action android:name="android.intent.action.delete.hide"/> 
<category android:name="android.intent.category.default"/> 
<data android:scheme="package"/> 
</intent-filter> 
</activity>

3、实现pakkageinstallactivityhide.java,uninstalleractivityhide.java。 只需把pakkageinstallactivity.java修改去掉dialog和对话框。

4、安装程序调用者发一个上面定义的intent即可。例如,静默安装/sdcard/hello.apk(卸载的方法类似)。

intent install_hide_intent = new intent("android.intent.action.view.hide"); 
install_hide_intent .setdataandtype(uri.parse("[file:///sdcard/hello.apk](file:///sdcard/hello.apk)"),
"application/vnd.android.package-archive"); 
startactivityforresult(install_hide_intent, install_rusult);

5、注意,这个方法需要packageinstall.apk与系统一起编译。这个apk在/system/app/目录下面;android.intent.action.view.hide 这个静默安装的接口需要开放给第三方。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网