Android 学习

Android 学习笔记总结

缘由

记录这篇文章缘起于需要完成一个嵌入式的作业,接触安卓还是在大二的时候,现在想起来应该用久远来形容了吧,哈哈。。。也差不多都是从头学啦,不过也挺好的有些东西用的时候再学习,短期内的成长有一种成就感--当然对个人而言咯。。。感谢vamei的指引,让我在短期内能够对Android有个大致了解,这篇文章也是参照vamei大神的博客而成.

Android简介

Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统、中间件、用户界面和应用软件组成。
正是因为开源使得Android迅速壮大,俺也越发感觉加入了开源系列的阵营,选择工具的时候会偏向跨平台的,发觉linux下命令行挺好用的,vim编辑器也不错,grep、awk简单粗暴。再说markdown,哎真是一用根本就停不下来啊。。。哈哈

Android架构

Android以linux2.6为基础 上面搭载了驱动程序、java虚拟机以及自己一套应用框架,分层的模式使得开发变得更加简单与高效,也使得应用开发人员只需要关注具体的应用设计,底层的东西都基本上封装好,提供高扩展性与兼容性。
应用程序运行在自己的虚拟机中DALvik,不同于传统的java虚拟机,采用的栈寄存器,一个应用程序的崩溃不会影响到整个系统,。。。具体再补。。。忘记了。。。囧

第一个应用

环境搭建:现在Android开发已经变得非常简单,之前的环境配置还要自己下载sdk,配置eclipse插件等。现在就可以直接去Android developmer 下载集成sdk的eclipse就可以了。

建立第一个应用程序。

用开发工具建好的会有一个fragment的布局文件,这是3.0后扩展的功能,但是感觉还是有点不大习惯,于是沿用之前的一个activitiy对应一个layout.xml布局的方式来进行。
上图就是建立好的hello工程,里面的文件夹不一一介绍,其中gen下是自动生成的,R里面的掌管着资源文件,代码中调用这些资源都是通过R.*.**来进行.
res下存放着各式各样的资源文件,包括图片、布局、字符串等文件。

配置流程
AndroidMainfest.xml配置应用程序的权限以及activities的一些相关信息

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.dcy.hello"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
```

下面是mainactivity

public class MainActivity extends Activity {
	private SharedPreferences sharedPref;
	private int count=0;
    TextView nameView ;
    ImageView img;
    int[] imageSrc = {R.drawable.a1, R.drawable.a2, R.drawable.a3,   
            R.drawable.a4, R.drawable.a5};  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPref = this.getSharedPreferences("edu.dcy.hello", 
                Context.MODE_PRIVATE);
        Button btn = (Button) findViewById(R.id.author);
        nameView= (TextView) findViewById(R.id.welcome);
        img=(ImageView) findViewById(R.id.imageView1);
        btn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                count++;
                img.setImageResource(imageSrc[count%5]);
                nameView.setText(count+"");
            }
        });
	}
	@Override
    protected void onResume() {
        super.onResume();
        TextView nameView = (TextView) findViewById(R.id.welcome);
        
        // retrieve content from shared preference, with key "name"
        String   welcome  = "Welcome, " + sharedPref.getString("name", "unknown") + "!";
        nameView.setText(welcome);
    }

}

activities_main.xml布局文件

<?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="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.58"
        android:src="@drawable/a1" />
    <TextView
        android:id="@+id/welcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="0.10" />
    <Button
        android:id="@+id/author"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me" />
</LinearLayout>

after done with this ,we will start up the application on the device
because if i run it in a stimlater ,it will be so slowly that i can not ren

组件简介

Activity
展现一些视觉元素,与用户交互。
Activity生命周期

Service
一般运行在后台,费时间长等特点。
Intent
传递消息或者上下文,用于activity之间的通信

Context
保存上下文信息,维护一些公用的东西。
生命周期

View
各种控件,用来展示

Resource
应用可能用到的资源,图片,字符串等

Activity间跳转传递信息

public class MainActivity extends Activity {

    ## share memory  , and is  key-value type
    
	private SharedPreferences sharedPref;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		sharedPref = this.getSharedPreferences("edu.dcy.hello",
				Context.MODE_PRIVATE);

		Button btn = (Button) findViewById(R.id.author);

		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
		    ## jump to another activity selfeditactivity
		    ## and we use the intent to  package the information
		    ## above is something to explain these codes
				Intent intent = new Intent(MainActivity.this,
						SelfEditActivity.class);
				MainActivity.this.startActivity(intent);
			}
		});

		......
		......
	}
}

SqlLite使用

sqllite作为一个内置的

WebView

ActivityBars

原文地址:https://www.cnblogs.com/thinkml/p/4170407.html