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

java实现静默安装apk

程序员文章站 2022-03-23 14:05:10
静默安装就是偷偷的把一个应用安装到手机上,就是屏蔽确认框,通过反射 只能写个主要的代码,这个是在linux编译用到,因为静默安装需要调用系统服务,在源码下编译,我也是搞了...

静默安装就是偷偷的把一个应用安装到手机上,就是屏蔽确认框,通过反射

只能写个主要的代码,这个是在linux编译用到,因为静默安装需要调用系统服务,在源码下编译,我也是搞了好久

installactivity.java

package com.smart.installback;
 
import java.io.file;
 
 
//import com.android.packageinstaller.r;
 
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.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;
 
public class installactivity extends activity {
  /** called when the activity is first created. */
  private button btn=null;
  private button btn2=null;
  private final int install_complete = 1;
  final static int succeeded = 1;
  final static int failed = 0;  
  public void oncreate(bundle savedinstancestate){
    super.oncreate(savedinstancestate);
     
    setcontentview(r.layout.main);
    string filename = environment.getexternalstoragedirectory() + "/uc.apk"; 
      uri uri = uri.fromfile(new file(filename));
 
      int installflags = 0;
      packagemanager pm = getpackagemanager();
      try {
        packageinfo pi = pm.getpackageinfo("com.uc.browser", 
            packagemanager.get_uninstalled_packages);
        if(pi != null) {
          installflags |= packagemanager.install_replace_existing;
        }
      } catch (namenotfoundexception e) {
      }
 
 
//      string installerpackagename = getintent().getstringextra(
//          intent.extra_installer_package_name);
      packageinstallobserver observer = new packageinstallobserver();
      pm.installpackage(uri, observer, installflags, "com.uc.browser");
  }
   
  private onclicklistener listener2=new onclicklistener(){
    public void onclick(view v) {
      string filename = environment.getexternalstoragedirectory() + "/uc.apk"; ivate 
      uri uri = uri.fromfile(new file(filename));
 
      int installflags = 0;
      packagemanager pm = getpackagemanager();
      try {
        packageinfo pi = pm.getpackageinfo("com.uc.browser", 
            packagemanager.get_uninstalled_packages);
        if(pi != null) {
          installflags |= packagemanager.install_replace_existing;
        }
      } catch (namenotfoundexception e) {
      }
         
       
//      string installerpackagename = getintent().getstringextra(
//          intent.extra_installer_package_name);
       
      packageinstallobserver observer = new packageinstallobserver();
      pm.installpackage(uri, observer, installflags, "com.uc.browser");
 
 
    }
  };
   
  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:
          // update the status text
          // show the ok button
          int centertextlabel;
          drawable centertextdrawable = null;
          if(msg.arg1 == succeeded) {
             
          } else {}
          if (centertextdrawable != null) {}
 
          break;
        default:
          break;
      }
    }
  };
  
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 
</linearlayout>

以上所述就是本文的全部内容了,希望大家能够喜欢。