package com.example.webgettest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.webkit.URLUtil; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class installTest extends AppCompatActivity { private TextView mTextView;//TextView控件的引用 private EditText mEditText;//EditText控件的引用 private Button mButton;//开始按钮 private static final String msg = "DOWNLOADAPK"; //定义异常抛出后的提示 private String currentFilePath = ""; //文件路径 private String currentTempFilePath = ""; //临时存盘文件路径 private String strURL="";//url的地址 private String fileEx=""; private String fileNa=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_install_test); mTextView = (TextView)findViewById(R.id.TextView01);//获取控件的Id mButton = (Button)findViewById(R.id.Button01);//获取控件的Id mEditText =(EditText)findViewById(R.id.EditText01);//获取控件的Id mButton.setOnClickListener(new Button.OnClickListener()//为控件添加监听 { public void onClick(View v) //当单击控件时 { mTextView.setText("下载中...");//显示下载中 strURL = mEditText.getText().toString(); //获取URL地址 fileEx = strURL.substring(strURL.lastIndexOf(".")+1, strURL.length()).toLowerCase();//取得Apk的路径 fileNa = strURL.substring(strURL.lastIndexOf("/")+1, strURL.lastIndexOf("."));//取得欲安装程序之文件名称 getFile(strURL);//调用getFile方法 } } ); mEditText.setOnClickListener(new EditText.OnClickListener() { @Override public void onClick(View arg0)//当单击EditText时 { mEditText.setText("");//清空EditText中的内容 mTextView.setText("请输入URL地址");//提示用户输入url地址 } }); } private void getFile(final String strPath) //getFile方法 { try { if (strPath.equals(currentFilePath) ) { getDataSource(strPath); } currentFilePath = strPath; //不满足条件,获取路径 Runnable r = new Runnable()//开一个线程 { public void run() { try { getDataSource(strPath); } catch (Exception e) { Log.e(msg, e.getMessage(), e); } } }; new Thread(r).start(); //开始线程 } catch(Exception e) { e.printStackTrace(); } } //取得远程文件 private void getDataSource(String strPath) throws Exception { if (URLUtil.isNetworkUrl(strPath))//如果是url地址 { URL myURL = new URL(strPath); //取得URL System.out.println("myURL="+myURL); URLConnection conn = myURL.openConnection(); //建立联机 conn.connect(); InputStream is = conn.getInputStream(); //InputStream 下载文件 if (is == null) //如果取得的数据为null { throw new RuntimeException("stream is null"); //如果为null,抛出异常 } File myTempFile = File.createTempFile(fileNa, "."+fileEx); //建立临时文件 currentTempFilePath = myTempFile.getAbsolutePath(); //取得临时存盘文件路径 FileOutputStream fos = new FileOutputStream(myTempFile); //将文件写入临时盘 byte buf[] = new byte[128]; do { System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); int numread = is.read(buf); if (numread <= 0) { break; } fos.write(buf, 0, numread); //将文件写入 }while (true); openFile(myTempFile);//打开文件进行安装 try { is.close();//关闭输入流 } catch (Exception ex) { Log.e(msg, "error: " + ex.getMessage(), ex); //抛出异常 } } else //如果不是url地址 { mTextView.setText("错误的URL"); //显示输入错误 } } private void openFile(File f) //在手机上打开文件的方法 { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); String type = getType(f);//调用getMIMEType()来取得Type intent.setDataAndType(Uri.fromFile(f),type);//设定intent的file与Type startActivity(intent); } //判断文件Type的方法 private String getType(File f) { String type=""; String fName=f.getName(); // 取得扩展名 String end=fName.substring(fName.lastIndexOf(".")+1,fName.length()).toLowerCase(); //按扩展名的类型决定MimeType if(end.equals("apk"))//判断扩展名是否是apk { type = "application/vnd.android.package-archive"; } else { type += "/*"; } return type; } //自定义删除文件方法 private void deletedFile(String strFileName) { File myFile = new File(strFileName);//获取文件名称 if(myFile.exists()) { myFile.delete();//如果存在,删除文件 } } //当Activity处于onPause状态时,更改TextView文字状态 @Override protected void onPause() { mTextView = (TextView)findViewById(R.id.TextView01); mTextView.setText("下载成功");//下载成功 super.onPause();//实现父类的方法 } //当Activity处于onResume状态时,删除临时文件 @Override protected void onResume() { deletedFile(currentTempFilePath);// 删除临时文件 super.onResume(); //实现父类的方法 } }