一个简单的 Android 示例组织结构图
Android 应用构成的组件的交互是怎么进行的,如图:
Android 开发流程
1.下载,安装ADT的Eclipse插件
第一步 在Eclipse3.4.1中,依次点Help-->Software Updates-->Avoilable Software-->Add Site -->Archive,此时将弹出一个窗口,选择ADT-0.9.1.zip,点OK,不要管弹出的错误窗口,点OK
第二步 展开jar:file:D/eclipse/ADT-0.9.1.zip/(这个目录地址是演示,根据ADT-0.9.1.zip实际目录选择) 勾选jar前的方框,点install,安装后重启Eclipse,安装成功后将出现一个手机图标!
2.下载,安装Android SDK插件
将android-sdk-windows-1.5_r3.zip解压到Eclipse安装目录下,
依次点Window-->Preferences-->Android,在右边SDK Location:文本框中选择解压后的randroid-sdk-window-1.5_r3目录,点确定,此时Eclipse将检测已安装的android插件,可能要重启Eclipse,一定要让Eclipse检测出来,检测出来后,点Apply,OK
------------------------------------------
接下来开发Android
1 点new-->other-->Android-->Android Project,点next
2 命名一个Project name,选择Android 1.5平台,在下方Application name中填写:HelloAndroid(示例)命名Package name,
在Create Activity(相当于struts的Action或web应用的servlet,用来处理用户请求) 文本框中填入:HelloActivity
在Min SDK Version填入3,表示应用的是Android 1.5
3 要运行程序,必须配置虚拟设备管理器(也就是虚拟手机),点 手机 符号,此时弹出Android Virtual Devices Manager 窗口
定义Name:test ,Target: Android 1.5 -1.5SDCard 暂时不填,Skin 选Default(HVGA),接着点Create AVD-->OK-->Finish
4 选中要运行的程序,右键点Run As-->Android Application,现在将出现一个手机
SDcard 存储过程中
一个读写类
package cn.action;
public class SDCardRW extends Activity {
private final static String FILENAME = "it.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText contentText = (EditText)findViewById(R.id.content);
String content = contentText.getText().toString();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//把文件保存在SDCard的根目录
File saveFile = new File(Environment.getExternalStorageDirectory(),FILENAME);
try{
//写文件
FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
fileOutputStream.write(content.getBytes("UTF-8"));
fileOutputStream.close();
Toast.makeText(SDCardRW.this, getString(R.string.info), Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}
}
});
Button readbutton = (Button)this.findViewById(R.id.readbutton);
readbutton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
TextView showView = (TextView)findViewById(R.id.show);
File readFile = new File(Environment.getExternalStorageDirectory(),FILENAME);
//读取文件
try {
FileInputStream fis = new FileInputStream(readFile); //读文件
ByteArrayOutputStream os = new ByteArrayOutputStream(); //写文件
byte[] buffer = new byte[1024];
int length = -1;
while((length = fis.read(buffer, 0, 1024))!= -1){
//写进去
os.write(buffer,0,length);
}
os.flush();
//将读取到的内容放到 content 数组中
byte[] content =os.toByteArray();
os.close();
fis.close();
showView.setText(new String(content,"UTF-8")); //显示在show组件上
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
Button download = (Button)this.findViewById(R.id.download);
download.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//首先判断SDCard是否存在
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
try{
EditText urlText = (EditText)findViewById(R.id.url);
String path = urlText.getText().toString();
String filename = path.substring(path.lastIndexOf("/")+1);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
File saveFile = new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream fos = new FileOutputStream(saveFile);
BufferedOutputStream bos = new BufferedOutputStream(fos,3072);
byte[] buffer = new byte[1024];
int length = -1;
while((length = is.read(buffer, 0, 1024))!= -1){
//写进去
bos.write(buffer,0,length);
}
bos.flush();
bos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
页面布局 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minLines="3" android:id="@+id/content"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/button" android:id="@+id/button"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@+id/show" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/readfile" android:id="@+id/readbutton" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minLines="3" android:id="@+id/url" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/download" android:id="@+id/download " />
</LinearLayout>