欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现静默安装与卸载的方法

程序员文章站 2022-05-16 12:24:03
本文实例讲述了android实现静默安装与卸载的方法。分享给大家供大家参考。具体如下: 方法1:【使用调用接口方法,由于安装卸载应用程序的部分api是隐藏的,所以必须下载...

本文实例讲述了android实现静默安装与卸载的方法。分享给大家供大家参考。具体如下:

方法1:【使用调用接口方法,由于安装卸载应用程序的部分api是隐藏的,所以必须下载android系统源码,在源码下开发并编译之后使用mm命令编译生成apk文件】

import java.io.file;
import android.app.activity;
import android.os.bundle;
import android.content.intent;
import android.content.pm.packageinfo;
import android.content.pm.packagemanager;
import android.content.pm.packagemanager.namenotfoundexception;
import android.content.pm.ipackageinstallobserver;
import android.content.pm.ipackagedeleteobserver;
import android.content.res.resources;
import android.graphics.drawable.drawable;
import android.net.uri;
import android.os.environment;
import android.os.handler;
import android.os.message;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.progressbar;
import android.widget.textview;
import android.widget.toast;
public class installactivity extends activity {
 /** called when the activity is first created. */
 private string apk_name = "qqhd-mini.apk";
 private button install = null;
 private button uninstall = null;
 private final int install_complete = 1;
 private final int uninstall_complete = 2;
 final static int succeeded = 1;
 final static int failed = 0;
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.install_uninstall);
  install = (button) findviewbyid(r.id.install);
 uninstall = (button) findviewbyid(r.id.uninstall);
 uninstall.setonclicklistener(listener1);
  install.setonclicklistener(listener2);
}
 private onclicklistener listener2=new onclicklistener(){
  public void onclick(view v) {
   string filename = environment.getexternalstoragedirectory() + "/" + apk_name; 
   uri uri = uri.fromfile(new file(filename));
   int installflags = 0;
   packagemanager pm = getpackagemanager();
   try {
    packageinfo pi = pm.getpackageinfo("com.tencent.minihd.qq", 
      packagemanager.get_uninstalled_packages);
    if(pi != null) {
     installflags |= packagemanager.install_replace_existing;
    }
   } catch (namenotfoundexception e) {}
   packageinstallobserver observer = new packageinstallobserver();
   pm.installpackage(uri, observer, installflags, "com.tencent.minihd.qq");
  }
 };
 class packageinstallobserver extends ipackageinstallobserver.stub {
  public void packageinstalled(string packagename, int returncode) {
   message msg = mhandler.obtainmessage(install_complete);
   msg.arg1 = returncode;
   mhandler.sendmessage(msg);
  }
 };
 private handler mhandler = new handler() {
  public void handlemessage(message msg) {
   switch (msg.what) {
    case install_complete:
     int centertextlabel;
     drawable centertextdrawable = null;
     if(msg.arg1 == succeeded) {
      toast.maketext(getapplicationcontext(), "install apk succeeded", toast.length_short).show();
     } else {}
     if (centertextdrawable != null) {}
     break;
 case uninstall_complete:
     int centertextlabel2;
     drawable centertextdrawable2 = null;
     if(msg.arg1 == succeeded) {
      toast.maketext(getapplicationcontext(), "uninstall apk succeeded", toast.length_short).show();
     } else {}
     if (centertextdrawable2 != null) {}
     break; 
    default:
     break;
   }
  }
 };
 private onclicklistener listener1=new onclicklistener(){
  public void onclick(view v) {
   packagemanager pm = getpackagemanager();
   packagedeleteobserver observer = new packagedeleteobserver();
   pm.deletepackage("com.tencent.minihd.qq", observer, 0);
  }
 };
 private class packagedeleteobserver extends ipackagedeleteobserver.stub {
  public void packagedeleted(string packagename, int returncode) {
   message msg = mhandler.obtainmessage(uninstall_complete);
   msg.arg1 = returncode;
   mhandler.sendmessage(msg);
  } 
 }
}

安装、卸载相关权限:

<uses-permission android:name="android.permission.write_external_storage"/>
<uses-permission android:name="android.permission.install_packages" />
<uses-permission android:name="android.permission.delete_packages" />
<uses-permission android:name="android.permission.clear_app_cache" />
<uses-permission android:name="android.permission.read_phone_state"/>
<uses-permission android:name="android.permission.clear_app_user_data" />

方法2:【需要获取root权限或者和rom拥有一样的签名,即让该apk以system的身份在系统中运行】

private void test() {
 execcommand("system/bin/pm install -r " + "sdcard/xxx.apk");
 // xxx.apk放置在内置储存的根目录下(mnt/sdcard/xxx.apk)
 // execcommand("system/bin/pm uninstall " + package_name); 
 //package_name为xxx.apk包名
 }
 public boolean execcommand(string cmd) {
 process process = null;
 try {
 process = runtime.getruntime().exec(cmd);
 process.waitfor();
 } catch (exception e) {
 return false;
 } finally {
 try {
 process.destroy();
 } catch (exception e) {
 }
 }
 return true;
}

其他方法:一些第三方jar包等方式。

希望本文所述对大家的android程序设计有所帮助。