• 下载完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();
     
            }
     
        }
     
    }
  • 相关阅读:
    附加数据库 对于 服务器“00-PC”失败
    SQL 语句转换格式函数Cast、Convert
    sql语句:union
    ISNULL-sqlserver语句
    SQL中的CASE WHEN语句
    SQL SELECT INTO 语句
    Sql语句中IN等方面的用法
    combobox的不常用的方法和将txt文本内容加到textbox中显示
    程序员:“菜鸟”和“大神”差距在哪
    过劳死离我们有多远?
  • 原文地址:https://www.cnblogs.com/zhengyanyan/p/5457367.html
Copyright © 2020-2023  润新知