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);
}
}
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);
}
}