package com.example.wps;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button download;
private Button show;
private TextView tv;
private Button wps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
download=(Button)findViewById(R.id.download);
show=(Button)findViewById(R.id.show);
wps=(Button)findViewById(R.id.wps);
tv=(TextView)findViewById(R.id.tv);
download.setOnClickListener(this);
show.setOnClickListener(this);
wps.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.download:
new Thread(new Runnable() {
@Override
public void run() {
HttpClient client=new DefaultHttpClient();
String url="http://www.baidu.com/";
HttpGet request=new HttpGet(url);
try {
HttpResponse response=client.execute(request);
if(response.getStatusLine().getStatusCode()==200){
InputStream is=response.getEntity().getContent();
int len=0;
byte[] read=new byte[1024];
File file=new File(Environment.getExternalStorageDirectory(),"baidu.txt");
if (!file.exists()) {
file.createNewFile();
}else{
Looper.prepare();
Toast.makeText(MainActivity.this, "文件夹存在",0).show();
Log.i("file", file.getAbsolutePath());
Looper.loop();
}
FileOutputStream fos=new FileOutputStream(file);
while((len=is.read(read))!=-1){
fos.write(read, 0, len);
}
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.show:
File file=new File(Environment.getExternalStorageDirectory(),"baidu.txt");
if(file.exists()){
try {
FileInputStream fis=new FileInputStream(file);
int len=0;
byte[] buffer=new byte[1024];
ByteArrayOutputStream baos=new ByteArrayOutputStream();
while ((len=fis.read(buffer))!=-1) {
baos.write(buffer,0,len);
}
fis.close();
baos.close();
tv.setText(new String(baos.toByteArray(),"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}else{
Toast.makeText(this, "file不存在", 0).show();
}
break;
case R.id.wps:
File file2=new File(Environment.getExternalStorageDirectory(),"baidu.txt");
if(file2.exists()){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file2);
intent.setDataAndType(uri, "application/msword");
//intent.setComponent(new ComponentName("cn.wps.moffice", "cn.wps.moffice.documentmanager.PreStartActivity"));
startActivity(intent);
}
break;
default:
break;
}
}
}
效果图