• 下载完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();
     
            }
     
        }
     
    }
  • 相关阅读:
    python变量赋值(可变与不可变)
    cx_Oracle读取中文乱码问题(转载)
    Lookandsay sequence(看读序列)
    oracle 效率之 not in 和exists
    python encode和decode函数说明
    PILpython的图像处理模块
    iOS中判断一个文件夹是否存在
    Validate Email Account using Regular Expression in ObjectiveC
    UILabel描边
    获取app当前可用的剩余内存
  • 原文地址:https://www.cnblogs.com/zhengyanyan/p/5457367.html
Copyright © 2020-2023  润新知