zoukankan      html  css  js  c++  java
  • 下载完apk安装包后实现自动安装;

    需配置的权限;

    <uses-permission android:name="android.permission.INTERNET" />

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
       <span style="color: #ff6600;"> <uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
    </span>

    代码实现;

    package com.exmple.httpxutil;

     
    import java.io.File;
     
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.content.pm.Signature;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;
     
    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.HttpHandler;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;
     
    public class MainActivity extends Activity {
        HttpHandler<File> h;
        private ProgressBar pa;
     
        @SuppressLint("SdCardPath")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button = (Button) findViewById(R.id.button);
            pa = (ProgressBar) findViewById(R.id.pro);
            Button more = (Button) findViewById(R.id.more);
            getCode();
            more.setOnClickListener(new OnClickListener() {
     
                @Override
                public void onClick(View v) {
                    Intent ints = new Intent(MainActivity.this, NewActivity.class);
                    startActivity(ints);
     
                }
            });
            button.setOnClickListener(new OnClickListener() {
     
                @Override
                public void onClick(View v) {
     
                    HttpUtils http = new HttpUtils();
                    final String path = Environment.getExternalStorageDirectory()
                            .getPath();
                    System.out.println(path);
     
                    h = http.download(
                            "http://101.200.142.201:8080/tqyb/baidumap.apk", path
                                    "/badumap.apk"truetrue,
                            new RequestCallBack<File>() {
                                @Override
                                public void onStart() {
                                    System.out.println("===");
                                    System.out.println("开始下载了++++++++++++++");
                                }
     
                                @Override
                                public void onLoading(long total, long current,
                                        boolean isUploading) {
                                    System.out.println(total + "=====");
                                    System.out.println(current + "=====");
                                    System.out.println(isUploading + "=====");
                                    pa.setMax((int) total);
                                    pa.setProgress((int) current);
                                }
     
                                @Override
                                public void onFailure(HttpException error,
                                        String msg) {
                                    System.out.println(error + "+++++++" + msg
                                            "+++++++++");
     
                                }
     
                                @Override
                                public void onSuccess(
                                        ResponseInfo<File> responseInfo) {
                                    h.cancel();
                                    installApk(path + "/badumap.apk");
     
                                }
     
                            });
     
                }
            });
     
        }
    //安装的方法
        private void installApk(String filename) {
            File file = new File(filename);
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            String type = "application/vnd.android.package-archive";
            intent.setDataAndType(Uri.fromFile(file), type);
            startActivity(intent);
     
        }
    //等到版本号的代码
        private void getCode() {
     
            PackageManager manager;
     
            PackageInfo info = null;
     
            manager = this.getPackageManager();
     
            try {
     
                info = manager.getPackageInfo(this.getPackageName(), 0);
                String name = info.versionName;
                int versionCode = info.versionCode;
                String packageName = info.packageName;
                Signature[] signatures = info.signatures;
                System.out.println(name + "++++++++++" + versionCode + "+++++"
                        + packageName + "========nnn" + signatures
                        "=============");
                /*
                 * info.versionName;
                 *
                 * info.packageName;
                 *
                 * info.signatures;
                 */
     
            catch (NameNotFoundException e) {
     
                // TODO Auto-generated catch block
     
                e.printStackTrace();
     
            }
     
        }
     
    }
  • 相关阅读:
    VS2010 VC Project的default Include设置
    Linux 下的编辑/编译器
    用命令实现Win7远程桌面关机和重启
    怎样快速刪除Word中超链接?
    chrome浏览器世界之窗浏览器的收藏夹在哪?
    代码量查找工具[最好用的]
    C项目实践--网络协议和套接字编程
    memmove 和 memcopy
    bzoj2456: mode
    bzoj1205: [HNOI2005]星际贸易
  • 原文地址:https://www.cnblogs.com/zhengyanyan/p/5457367.html
Copyright © 2011-2022 走看看