热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

java实现静默安装apk

本文给大家分享的是如何实现偷偷的把一个安卓apk应用安装到手机上,而且不显示确认框,主要是通过反射来实现,好了,小伙伴们仔细看下代码吧,有需要的小伙伴可以参考下。

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

只能写个主要的代码,这个是在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

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 

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


推荐阅读
author-avatar
多米音乐_35677591
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有