代码中执行apk安装、卸载、更新以及静默安装
来源:互联网 发布:格子衬衫 知乎 编辑:程序博客网 时间:2024/05/24 06:00
import java.io.File;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button installBtn = (Button)findViewById(R.id.install_btn); installBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(new File("/sdcard/xxx.apk")), "application/vnd.android.package-archive");MainActivity.this.startActivity(intent);}}); Button uninstallBtn = (Button)findViewById(R.id.uninstall_btn); uninstallBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Uri uri = Uri.parse("package:com.xxx.xxx(包名)");Intent intent2 = new Intent(Intent.ACTION_DELETE, uri); MainActivity.this.startActivity(intent2);}}); }}
上述安装不仅可以安装新的apk,也适用于更新旧的apk
更新apk必须保证新旧apk签名一致。
对于data分区的外部应用更新:
新的apk覆盖旧的apk
对于systen系统分区的内置应用更新:
system/app/下的旧apk仍然存在,新的apk被复制到data/app下。虽然同时存在两新旧apk,但运行时系统选择data分区的新apk运行!
如果执行卸载则是删除data分区的apk,再次启动程序运行的是system的旧apk
静默安装
静默安装apk时,程序需要平台签名(使用signapk),
singapk命令行签名
#java -jar signapk.jar platform.x509.pem platform.pk8 xxx.apk xxx-sign.apk
如果要将apk安装在system分区下还要在mantifest中加入系统权限
<mantifest
...
android:sharedUserId="android.uid.system" >
uses-permission也需要安装apk的权限
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
eclipse工程需要引入framework.jar
自定义MyPackageInstallObserver类继承自IPackageInstallObserver.Stub
public void installOnBackground(File file){Log.i(TAG, "installOnBackground()");int installFlags = 0;Uri packageUri = Uri.fromFile(file);//file是要安装的apk文件 PackageManager pm = mContext.getPackageManager();installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;MyPackageInstallObserver observer = new MyPackageInstallObserver();pm.installPackage(packageUri, observer, installFlags, "com.xxx.xxx包名"); }private static class MyPackageInstallObserver extends IPackageInstallObserver.Stub{@Overridepublic void packageInstalled(String packageName, int returnCode)throws RemoteException {//returnCode=1表示安装成功Log.e(TAG, "packageInstalled()");Log.i(TAG, "packageName = " + packageName);Log.i(TAG, "returnCode = " + returnCode);}}
- 代码中执行apk安装、卸载、更新以及静默安装
- 代码中执行apk安装、卸载、更新以及静默安装
- 代码中执行apk静默安装
- 静默安装apk,静默卸载apk
- android静默安装、静默卸载apk方法
- android 中静默安装APK实现代码
- APK静默安装与更新
- android apk静默安装和卸载
- android apk静默安装和卸载
- Android静默安装和卸载APK
- APK root权限静默安装、卸载
- android apk静默安装和卸载(2)
- android apk静默安装和卸载
- 静默安装和卸载第三方apk
- 转载 :Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- 关于平均值(By Dahua Lin@MIT)
- 官方Android源码编译提示
- Oracle 左连接,右连接,全连接,sql实例
- 语音识别 TTS
- Aspose.Cells教学视频
- 代码中执行apk安装、卸载、更新以及静默安装
- 基类与派生类对象的关系
- matlab中自定义文件名和系统自带文件名重复的处理
- 老调重弹:年龄大了,码农何去何从
- TreeCtrl控件,选择根节点时子节点同时被选的代码
- Parsley开发指南 5 解耦绑定
- Project configuration is not up-to-date with pom.xm
- 回车与换行 ASCII
- C#语法糖(Csharp Syntactic sugar)大汇总