Android如何实现文件下载并自动安装apk包

public class MainActivity extends Activity {
 
        private String currentFilePath = "", currentTempFilePath = "", strURL = "",
                        fileEx = "", fileNa = "";
        File file2 = new File(Environment.getExternalStorageDirectory()+"");
        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                fragmentLayout = (View) findViewById(R.id.fragment);
                gotoButton = (Button) findViewById(R.id.goto_button);
                gotoButton.setOnClickListener(gotoListener);
        }
 
        OnClickListener gotoListener = new OnClickListener() {
 
                @Override
                public void onClick(View v) {
 
                        // String webUrl = "http://fancy.189.cn/portal/getclientapk";
                        // Uri uri = Uri.parse(webUrl);
                        // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        // startActivity(intent);
                        // finish();
                        strURL = "http://fancy.189.cn/portal/getclientapk";
                        /* 取得欲安装程序名称 */
                        fileEx = "telecom_mdesk";
                        fileNa = ".apk";
                        getFile(strURL);
                }
        };
 
        /* 处理下载URL文件自定义函数 */
        private void getFile(final String strPath) {
                try {
                        if (strPath.equals(currentFilePath)) {
                                getDataSource(strPath);
                        }
                        currentFilePath = strPath;
                        Runnable r = new Runnable() {
                                public void run() {
                                        try {
                                                getDataSource(strPath);
                                        } catch (Exception e) {
                                                Log.e("TAG", e.getMessage(), e);
                                                Log.e("TAG", "------>");
                                        }
                                }
                        };
                        new Thread(r).start();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        /* 取得远程文件 */
        private void getDataSource(String strPath) throws Exception {
                // if (!URLUtil.isNetworkUrl(strPath))
                // mTextView01.setText("错误的URL");
                // else
                // {
                /* 取得URL */
                URL myURL = new URL(strPath);
                /* 创建连接 */
                //URLConnection conn = myURL.openConnection();
                HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
                conn.setRequestMethod("POST");   
                // is = connection.getInputStream();
                //conn.connect();
                /* InputStream 下载文件 */
                InputStream is = conn.getInputStream();
                if (is == null)
                        throw new RuntimeException("stream is null");
 
                /* 创建临时文件 */
                File myTempFile = File.createTempFile(fileEx, fileNa, file2);
                /* 取得临时文件路径 */
                currentTempFilePath = myTempFile.getAbsolutePath();
                /* 将文件写入暂存盘 */
                FileOutputStream fos = new FileOutputStream(myTempFile);
                byte buf[] = new byte[128];
                do {
                        int numread = is.read(buf);
                        if (numread <= 0)
                                break;
                         
                        Log.v("TAG", "下载中---");
 
                        fos.write(buf, 0, numread);
                } while (true);
 
                /* 打开文件进行安装 */
                openFile(myTempFile);
                try {
                        is.close();
                } catch (Exception ex) {
                        Log.e("TAG", "error: " + ex.getMessage(), ex);
                }
                // }
        }
 
        /**
         * 在手机上打开文件
         */
        private void openFile(File f) {
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.content.Intent.ACTION_VIEW);
 
                /* 调用getMIMEType()来取得MimeType */
                String type = "application/vnd.android.package-archive";
                /* 设置intent的file与MimeType */
                intent.setDataAndType(Uri.fromFile(f), type);
                startActivity(intent);
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值