下载弹出确认窗口,避免自动下载

This commit is contained in:
sonichy 2019-03-30 22:16:37 +08:00
parent 963f99f656
commit 415b896d50
7 changed files with 231 additions and 7 deletions

BIN
app.apk

Binary file not shown.

View File

@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hty.browser"
android:versionCode="4"
android:versionName="4.1">
android:versionName="4.2">
<uses-sdk
android:minSdkVersion="16"

View File

@ -9,7 +9,7 @@ p { text-indent:2em; }
</style>
</head>
<body>
<h2 align=center><img src=logo.png><br>海天鹰浏览器 V4.1</h2>
<h2 align=center><img src=logo.png><br>海天鹰浏览器 V4.2</h2>
<p>内置扩展的浏览器。</p>
<p>扩展:链接关键字过滤,链接关键字高亮,图片自定义过滤,视频独立播放。</p>
<p>作者:黄颖</p>
@ -22,6 +22,7 @@ p { text-indent:2em; }
<p><a href="https://blog.csdn.net/lowprofile_coding/article/details/77852131" target="_blank">自动安装apk包</a></p>
<p><a href="https://stackoverflow.com/questions/3462582/display-the-android-webviews-favicon" target="_blank">获取网页图标</a></p>
<h3>更新日志:</h3>
<h3>V4.2 (2019-03-29)</h3><ol><li>下载弹出确认窗口,避免自动下载。</li></ol>
<h3>V4.1 (2019-03-05)</h3><ol><li>链接的 target=_blank 在新窗口中打开。</li><li>淘宝协议自动跳转淘宝APP。</li></ol>
<h3>V4.0 (2019-03-02)</h3><ol><li>实现多窗口。</li></ol>
<h3>V3.12 (2019-02-27)</h3><ol><li>支持跨域。</li><li>获取网页图标。</li><li>屏蔽百度首页视频百度好看跳转下载。</li><li>Android API 19 以上开启调试,运行 adb devices 连接手机Chrome 浏览器 chrome://inspect 查看。</li><li>增加标题按钮,点击标题按钮显示地址栏,点击网页隐藏地址栏。</li></ol>

View File

@ -11,7 +11,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -45,6 +47,7 @@ import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;
@ -55,6 +58,7 @@ import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
@ -91,8 +95,8 @@ import android.widget.Toast;
public class MainActivity extends Activity {
Button button_title, button_page;
TextView textView_searchCount;
EditText editText1, editText_search;
TextView textView_searchCount, textView_filesize;
EditText editText1, editText_search, editText_download_path;
ImageButton imageButton_go, imageButton_back, imageButton_forward, imageButton_menu, imageButton_searchPrev, imageButton_searchNext, imageButton_searchClose, imageButton_info;
// RelativeLayout RelativeLayout1;
LinearLayout LinearLayout1, LinearLayout2;
@ -111,6 +115,7 @@ public class MainActivity extends Activity {
long downloadIdUpdate;
List<WebView> list_webView = new ArrayList<>();
int currentPage;
int FILECHOOSER_DOWNLOAD_PATH = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -370,7 +375,8 @@ public class MainActivity extends Activity {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.e(Thread.currentThread().getStackTrace()[2] + "", url);
downloadBySystem(url, "", "");
//downloadBySystem(url, "", "");
dialog_new_download(url, "", "");
}
}
@ -415,7 +421,8 @@ public class MainActivity extends Activity {
mClipboard.setPrimaryClip(theClip);
break;
case 2:
downloadBySystem(HTRE,"","");
//downloadBySystem(HTRE, "", "");
dialog_new_download(HTRE, "", "");
break;
case 3:
ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
@ -505,6 +512,29 @@ public class MainActivity extends Activity {
if (intent != null)
list_webView.get(currentPage).loadUrl(intent.getStringExtra("url"));
}
if (requestCode == FILECHOOSER_DOWNLOAD_PATH) {
if (resultCode == Activity.RESULT_OK) { //是否选择没选择就不会继续
Uri uri = intent.getData(); // 得到uri后面就是将uri转化成file的过程
//String scheme = uri.getScheme();
Log.e("uri", uri.toString());
String[] projection = { "_data" };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if(cursor != null) {
int column_index = cursor.getColumnIndexOrThrow("_data");
cursor.moveToFirst();
String filepath = cursor.getString(column_index);
Log.e(Thread.currentThread().getStackTrace()[2] + "", filepath);
int endIndex = filepath.lastIndexOf("/");
if (endIndex != -1) {
String path = filepath.substring(0, endIndex);
Log.e(Thread.currentThread().getStackTrace()[2] + "", path);
//Toast.makeText(MainActivity.this, path, Toast.LENGTH_SHORT).show();
editText_download_path.setText(path);
}
}
}
}
}
private class SaveImage extends AsyncTask<String, Void, String> {
@ -1331,4 +1361,105 @@ public class MainActivity extends Activity {
}
}
void dialog_new_download(final String surl, String contentDisposition, String mimeType){
View view = LayoutInflater.from(this).inflate(R.layout.dialog_new_download, null, false);
final EditText editText_download_url = (EditText) view.findViewById(R.id.editText_download_url);
editText_download_url.setText(surl);
EditText editText_download_filename = (EditText) view.findViewById(R.id.editText_download_filename);
editText_download_filename.setText(surl.substring(surl.lastIndexOf("/")+1));
editText_download_path = (EditText) view.findViewById(R.id.editText_download_path);
//String path = Environment.getExternalStorageDirectory().getPath() + "/download";
String path = Environment.DIRECTORY_DOWNLOADS;
editText_download_path.setText(path);
ImageButton imageButton_path = (ImageButton) view.findViewById(R.id.imageButton_path);
imageButton_path.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, FILECHOOSER_DOWNLOAD_PATH);
}
});
textView_filesize = (TextView) view.findViewById(R.id.textView_filesize);
GetFileLengthThread getFileLengthThread = new GetFileLengthThread();
getFileLengthThread.surl = editText_download_url.getText().toString();
getFileLengthThread.start();
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("新建下载")
.setIcon(android.R.drawable.stat_sys_download)
.setView(view)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_SHORT).show();
String url = editText_download_url.getText().toString();
if(!url.equals("")) {
downloadBySystem(url, "", "");
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what){
case 0:
textView_filesize.setText(formatFileSize(msg.arg1));
break;
}
};
};
class GetFileLengthThread extends Thread{
String surl;
public void run(){
int fileLength = 0;
URL url = null;
try {
url = new URL(surl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection urlcon;
try {
urlcon = (HttpURLConnection) url.openConnection();
fileLength = urlcon.getContentLength();
} catch (IOException e) {
e.printStackTrace();
}
Message message = Message.obtain();
message.what = 0;
message.arg1 = fileLength;
handler.sendMessage(message);
}
}
String formatFileSize(int fileLength){
DecimalFormat DF = new DecimalFormat("#.00");
String filesize = "";
if (fileLength > 1000000000) {
filesize = DF.format(fileLength/(1024*1024*1024)) + " GB";
} else if(fileLength > 1000000) {
filesize = DF.format(fileLength/(1024*1024)) + " MB";
} else if (fileLength > 1000) {
filesize = DF.format(fileLength/1024) + " KB";
} else{
filesize = fileLength + " B";
}
return filesize;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:columnCount="3">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="下载地址:"/>
<EditText
android:id="@+id/editText_download_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:ems="10"
android:singleLine="true"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:text="文件名称:"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:text="保存路径:"/>
<EditText
android:id="@+id/editText_download_filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:ems="10"
android:singleLine="true"/>
<EditText
android:id="@+id/editText_download_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:ems="10"
android:singleLine="true"/>
<ImageButton
android:id="@+id/imageButton_path"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_row="2"
android:layout_gravity="center"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/folder_download"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:layout_gravity="center"
android:text="文件大小:"/>
<TextView
android:id="@+id/textView_filesize"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="3"
android:layout_column="1"
android:layout_margin="5dp"/>
</GridLayout>
</LinearLayout>

View File

@ -1 +1 @@
4.1
4.2