没有合适的资源?快使用搜索试试~ 我知道了~
Android项目源码.pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 188 浏览量
2021-10-02
11:14:02
上传
评论
收藏 56KB PDF 举报
温馨提示
试读
22页
Android项目源码.pdf
资源推荐
资源详情
资源评论
项目一: activity 之间的传递
public class Activity01 extends Activity {
/** Called when the activity is first created. */
Button myButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("test", "today is wongsdeasdf");
intent.setClass(Activity01.this, OthereActivity.class);
Activity01.this.startActivity(intent);
}
}
}
第二个 ACTIVITY
public class OthereActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView myTextView = (TextView)this.findViewById(R.id.myTextView);
Intent intent = this.getIntent();
String value = intent.getStringExtra("test");
System.out.println(value);
myTextView.setText(value);
}
}
项目二:关于文件下载
public class DownloadActivity extends Activity {
private Button downloadTextButton ;
private Button downloadMp3Button ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. main );
downloadTextButton = (Button)findViewById(R.id. lrc );
downloadTextButton .setOnClickListener( new
DownloadTxtListener());
downloadMp3Button = (Button)findViewById(R.id. MP3);
downloadMp3Button .setOnClickListener( new
DownloadMp3Listener());
}
class DownloadTxtListener implements
android.view.View.OnClickListener{
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
String lrc =
httpDownloader.download( "http://192.168.1.105:8080/mp3/Far Away From
Home.lrc" );
System. out .println(lrc);
}
}
class DownloadMp3Listener implements
android.view.View.OnClickListener{
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
int result =
httpDownloader.downFile( "http://192.168.1.105:8080/mp3/Far Away From
Home.wma" , "mp3/" , "Far Away From Home.wma" );
System. out .println(result);
}
}
}
Utiles 类:
public class FileUtils {
private String SDPATH;
public String getSDPA TH() {
return SDPATH;
}
public FileUtils() {
//得到当前外部存储设备的目录
// /SDCARD
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在 SD 卡上创建文件
*
* @throws IOException
*/
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPA TH + fileName);
file.createNewFile();
return file;
}
/**
* 在 SD 卡上创建目录
*
* @param dirName
*/
public File creatSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdirs();
return dir;
}
/**
* 判断 SD 卡上的文件夹是否存在
*/
public boolean isFileExist(String fileName){
File file = new File(SDPA TH + fileName);
return file.exists();
}
/**
* 将一个 InputStream 里面的数据写入到 SD 卡中
*/
public File write2SDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
creatSDDir(path);
file = creatSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer [] = new byte[4 * 1024];
while((input.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
output.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
}
}
Downloader 帮助类
public class HttpDownloader {
private URL url = null;
/**
* 根据 URL 下载文件, 前提是这个文件当中的内容是文本, 函数的返回值就是文件当
中的内容
* 1. 创建一个 URL 对象
* 2. 通过 URL 对象,创建一个 HttpURLConnection 对象
* 3. 得到 InputStram
* 4. 从 InputStream 当中读取数据
* @param urlStr
* @return
*/
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
buffer = new BufferedReader(new
InputStreamReader(getInputStreamFromUrl(urlStr)));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存
在
*/
public int downFile(String urlStr, String path, String fileName) {
InputStream inputStream = null;
try {
FileUtils fileUtils = new FileUtils();
if (fileUtils.isFileExist(path + fileName)) {
return 1;
} else {
剩余21页未读,继续阅读
资源评论
资料大全
- 粉丝: 14
- 资源: 26万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功