这次直接整个项目都复制过来了∩_∩,所以就不打注释了,可以直接复制使用 下面是代码 MainActivity.java代码
mport android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import com.mycompany.myapp.*;
public class MainActivity extends Activity
{
TextView jd;
EditText et,et2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void 下载(View p1)
{
et = (EditText)findViewById(R.id.T);
et2 = (EditText)findViewById(R.id.H);
String 标题 =et2.getText().toString();
String 下载链接 = et.getText().toString();
final AlertDialog dia = new AlertDialog.Builder(MainActivity.this).create();
dia.show();
dia.setCancelable(false);
Window window = dia.getWindow();
window.setContentView(R.layout.ailunxz);
WindowManager m = MainActivity.this.getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
WindowManager.LayoutParams params = window.getAttributes();
params.width = (int) (d.getWidth() * 0.8);
window.setAttributes(params);//此句代码一定要放在show()后面,否则不起作用
dia.setCanceledOnTouchOutside(false);
jd = (TextView)window.findViewById(R.id.jd);
down down=new down();
down.setondown(new down.ondown()
{
@Override
public void downing(int len, int oklen)
{
jd.setText("下载进度:" + (int)(((double)oklen / (double)len) * 100) + "%");
}
@Override
public void downok(String ruest)
{
dia.dismiss();
Toast.makeText(MainActivity.this, "下载完成\n/sdcard/Download", Toast.LENGTH_SHORT).show();
}
});
down.dowmFile(下载链接, "/mnt/sdcard/Download/" + 标题 );
}}
down.java代码
import android.os.Handler;
import android.os.Message;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class down
{
ondown dow;
String ruest;
int length=0,len=0;
public static interface ondown
{
public void downing(int len, int oklen);
public void downok(String ruest)
}
public void setondown(ondown dow)
{
this.dow = dow;
}
public void dowmFile(final String URL, final String path)
{
new Thread()
{
public void run()
{
String url=URL;
try
{
File ff=new File(path);
if (!ff.getParentFile().exists())
{
ff.getParentFile().mkdirs();
}
String rgx="(?<==)(.|\n)+?(?=&|$)";
Matcher su=Pattern.compile(rgx).matcher(url);
while (su.find())
{
String o=su.group();
url = url.replace(o, URLEncoder.encode(o));
}
String[] one={"{","}"};
for (String two:one)
{
url = url.replace(two, URLEncoder.encode(two));
}
URL UR=new URL(url);
URLConnection op=UR.openConnection();
InputStream input=op.getInputStream();
byte[] b=new byte[1024 * 1024];
int le=0;
length = op.getContentLength();
len = 0;
FileOutputStream hj=new FileOutputStream(path);
while ((le = input.read(b)) != -1)
{
len += le;
if (dow != null)
{
Message me=new Message();
me.what = 0;
hh.sendMessage(me);
}
hj.write(b, 0, le);
}
input.close();
hj.flush();
hj.close();
ruest = "下载完成";
}
catch (Exception e)
{
ruest = e.toString();
}
Message me=new Message();
me.what = 1;
hh.sendMessage(me);
}
}.start();
}
Handler hh=new Handler()
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case 0:
dow.downing(length, len);
break;
case 1:
dow.downok(ruest);
break;
}
}
};
}
ailunxz.xml布局
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="2dp">
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center">
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:textColor="#000000"
android:textSize="18sp"
android:layout_gravity="left|center"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="下载中"/>
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:gravity="left|center">
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="35dp"/>
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_marginLeft="30dp"
android:id="@+id/jd"/>
```xml
main.xml布局
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ems="10"
android:id="@+id/T"
android:hint="下载链接"/>
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ems="10"
android:layout_below="@id/T"
android:id="@+id/H"
android:hint="下载名字"/>
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="下载"
android:layout_below="@id/H"
android:onClick="下载"/>