Android学习总结

Android 编程五要诀:Activity、Service、BroadcastReceiver、ContentProvider、Intent中我们大概了解了这五种要素,今天就这五要素谈谈我的心得吧。

一、Activity

首先是生命周期,Activity的生命周期可以分为三组:

  • The entire lifetime of an activity happens between the first call toonCreate() through to a single final call to onDestroy().
  • The visible lifetime of an activity happens between a call toonStart() until a corresponding call to onStop().

  • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call toonPause().

activity_lifecycle

 

Activity有三种基本状态:

  1. Active:处于屏幕前景(当前task的栈顶Activity处于Active状态),同一时刻只能有一个Activity处于Active状态;
  2. Paused状态:处于背景画面画面状态,失去了焦点,但依然是活动状态;
  3. stopped:不可见,但依然保持所有的状态和内存信息。

可以调用finish()结束处理Paused或者stopped状态的Activity。

各种状态之间通过下列的函数调用转换:

void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()

保存Activity状态

To capture that state before the activity is killed, you can implement an onSaveInstanceState()method for the activity. Android calls this method before making the activity vulnerable to being destroyed — that is, before onPause() is called. It passes the method a Bundle object where you can record the dynamic state of the activity as name-value pairs. When the activity is again started, the Bundle is passed both to onCreate() and to a method that's called afteronStart(),onRestoreInstanceState(), so that either or both of them can recreate the captured state.

Unlike onPause() and the other methods discussed earlier, onSaveInstanceState() andonRestoreInstanceState()are not lifecycle methods. They are not always called. BecauseonSaveInstanceState() is not always called, you should use it only to record the transient state of the activity, not to store persistent data. Use onPause() for that purpose instead.

要保存activity被杀死前的状态,你可以执行onSaveInstanceState()方法。Android调用这个方法应该在这个activity变得比较容易被销毁之前-也就是在调用onPause()之前。它传递一个Bundle对象给这个方法,该对象使用名称-值对记录activity的动态状态。当activity再次被启动时,这个Bundle被传递给onCreate() 和一个在onStart()之后被调用的方法,onRestoreInstanceState(),这样,他们就能在重新创建的时候捕获状态了。

不同于onPause() 和之前讨论过的方法,onSaveInstanceState() 和onRestoreInstanceState()是没有生命周期的方法。他们不是每次都被调用。因为onSaveInstanceState() 没有每次被调用,你应该只使用它来记录activity的暂时状态,而不是存储永久数据。不过可以使用onPause() 代替。

启动另一个Activity的过程

  • The current activity's onPause() method is called.
  • Next, the starting activity's onCreate()onStart(), and onResume() methods are called in sequence.
  • Then, if the starting activity is no longer visible on screen, its onStop() method is called.

     

  • 当前activity的onPause() 被调用
  • 下一步,启动的activity的依次调用onCreate(), onStart(), and onResume()
  • 然后,如果启动的activity不再被显示,它的 onStop() 被调用

     

    Activity中常用的函数有SetContentView()   findViewById()    finish()   startActivity(),其生命周期涉及的函数有:

    void onCreate(Bundle savedInstanceState)
    void onStart()
    void onRestart()
    void onResume()
    void onPause()
    void onStop()
    void onDestroy()

    注意的是,Activity的使用需要在Manifest文件中添加相应的<Activity>,并设置其属性和intent-filter。

     

    借用代码为例:

  • AndroidManifest.xml程序清单

     

    Androidmanifest.xml代码    收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.oristand" android:versionCode="1" android:versionName="1.0.0">  
    4.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    5.         <activity android:name=".LoginDemoActivity" android:label="@string/app_name">  
    6.             <intent-filter>  
    7.                 <action android:name="android.intent.action.MAIN" />  
    8.                 <category android:name="android.intent.category.LAUNCHER" />  
    9.             </intent-filter>  
    10.         </activity>  
    11.         <!-- login activity -->  
    12.         <activity android:name=".LoginActivity" android:label="@string/app_login"></activity>  
    13.     </application>  
    14. </manifest>   

    如何改变Theme呢?在manifest里面将<application android:icon="@drawable/icon" android:label="@string/app_name">修改成:<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme">

     在前篇文章中提到的permissions又是什么呢?

    在<application。。。>之前添加如下permission,就有了执行一些功能的许可。在百度地图应用中,我们就需要添加如下的permissions。

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
        </uses-permission>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
        </uses-permission>
        <uses-permission android:name="android.permission.INTERNET" >
        </uses-permission>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
        </uses-permission>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
        </uses-permission>
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
        </uses-permission>
        <uses-permission android:name="android.permission.READ_PHONE_STATE" >
        </uses-permission>
    
        <supports-screens
            android:anyDensity="true"
            android:largeScreens="true"
            android:normalScreens="true"
            android:resizeable="true"
            android:smallScreens="true" />
    
    


    每个activity在manifest中都必须申明,如上面所写LoginDemoActivityLoginActivity

     

  •  string.xml程序清单

     

    String.xml代码    收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="app_name">LoginDemo</string>  
    4.     <string name="txt_btn_go">Go...</string>  
    5.   
    6.     <string name="app_login">登录</string>  
    7.     <string name="txt_username">用户名:</string>  
    8.     <string name="txt_passwd">密 码:</string>  
    9.     <string name="txt_btn_login">登 录</string>  
    10.     <string name="txt_btn_reset">取 消</string>  
    11. </resources>  

     

     LoginDemoActivity程序清单

     

    Logindemoactivity 代码    收藏代码
    1. package com.oristand;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Intent;  
    5. import android.os.Bundle;  
    6. import android.view.View;  
    7. import android.widget.Button;  
    8.   
    9. public class LoginDemoActivity extends Activity {  
    10.   
    11.     // 点击go按钮,进入登录活动(LoginActivity)  
    12.     private Button btn_go;  
    13.   
    14.     // 按钮添加监听事件  
    15.     private BtnListener btnListener = new BtnListener();  
    16.   
    17.     /** Called when the activity is first created. */  
    18.     @Override  
    19.     public void onCreate(Bundle savedInstanceState) {  
    20.         super.onCreate(savedInstanceState);  
    21.         setContentView(R.layout.main);  
    22.   
    23.         // 丛当前试图中找到按钮,初始化  
    24.         btn_go = (Button) findViewById(R.id.btn_go);  
    25.   
    26.         // 绑定点击事件  
    27.         btn_go.setOnClickListener(btnListener);  
    28.   
    29.     }  
    30.   
    31.     // 监听事件  
    32.     private class BtnListener implements View.OnClickListener {  
    33.         @Override  
    34.         public void onClick(View v) {  
    35.             // TODO Auto-generated method stub  
    36.             if (v.getId() == R.id.btn_go) {  
    37.                 login();  
    38.             }  
    39.         }  
    40.     }  
    41.                 // 通过Intent实现跳转  
    42.     public void login() {  
    43.         Intent intent_login = new Intent();  
    44.         intent_login.setClass(this, LoginActivity.class);  
    45.         startActivity(intent_login);  
    46.     }  
    47. }  

     

     

     LoginActivity.java程序清单

    Loginactivity.java代码    收藏代码
    1. package com.oristand;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.view.View;  
    6. import android.widget.Button;  
    7. import android.widget.EditText;  
    8.   
    9. public class LoginActivity extends Activity {  
    10.   
    11.     // 用户名输入框  
    12.     private EditText et_username;  
    13.   
    14.     // 密码输入框  
    15.     private EditText et_passwd;  
    16.   
    17.     // 登录按钮  
    18.     private Button btn_login;  
    19.   
    20.     // 取消按钮  
    21.     Button btn_reset;  
    22.   
    23.     // 按钮添加点击事件  
    24.     BtnListener btnListener = new BtnListener();  
    25.   
    26.     @Override  
    27.     protected void onCreate(Bundle savedInstanceState) {  
    28.         // TODO Auto-generated method stub  
    29.   
    30.         super.onCreate(savedInstanceState);  
    31.   
    32.         // 设置当前视图  
    33.         setContentView(R.layout.login);  
    34.   
    35.         // 从当前事件中找到输入框,初始化  
    36.         et_username = (EditText) findViewById(R.id.et_username);  
    37.         et_passwd = (EditText) findViewById(R.id.et_passwd);  
    38.   
    39.         // 从当前事件中找到登录按钮,初始化  
    40.         btn_login = (Button) findViewById(R.id.btn_login);  
    41.         btn_login.setOnClickListener(btnListener);  
    42.   
    43.         // ...  
    44.         btn_reset = (Button) findViewById(R.id.btn_reset);  
    45.         btn_reset.setOnClickListener(btnListener);  
    46.     }  
    47.   
    48.     // 点击事件  
    49.     private class BtnListener implements View.OnClickListener {  
    50.         @Override  
    51.         public void onClick(View v) {  
    52.             // TODO Auto-generated method stub  
    53.             if (v.getId() == R.id.btn_login) {  
    54.   
    55.                 // do login...  
    56.   
    57.             } else if (v.getId() == R.id.btn_reset) {  
    58.                 finish();  
    59.             }  
    60.         }  
    61.     }  
    62. }  

     

    main.xml程序清单

     

    Main.xml代码    收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical" android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal">  
    5.     <Button android:id="@+id/btn_go" android:layout_width="wrap_content"  
    6.         android:layout_height="wrap_content" android:text="@string/txt_btn_go" />  
    7. </LinearLayout>  
    8.   
    9.       

     

     

       login.xml程序清单

     

    Login.xml代码    收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical" android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent">  
    5.   
    6.     <TableRow android:orientation="horizontal"  
    7.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
    8.   
    9.         <TextView android:layout_width="fill_parent"  
    10.             android:layout_height="wrap_content" android:text="@string/txt_username" />  
    11.   
    12.         <EditText android:id="@+id/et_username" android:maxLength="8"  
    13.             android:maxLines="1" android:layout_weight="1.0"  
    14.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
    15.             android:text="" />  
    16.     </TableRow>  
    17.   
    18.     <TableRow android:orientation="horizontal"  
    19.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
    20.   
    21.         <TextView android:layout_width="fill_parent"  
    22.             android:layout_height="wrap_content" android:text="@string/txt_passwd" />  
    23.   
    24.         <EditText android:id="@+id/et_passwd" android:password="true"  
    25.             android:maxLength="10" android:maxLines="1" android:layout_weight="1.0"  
    26.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
    27.             android:text="" />  
    28.     </TableRow>  
    29.     <LinearLayout android:orientation="horizontal"  
    30.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
    31.   
    32.         <Button android:id="@+id/btn_login" android:layout_width="fill_parent"  
    33.             android:layout_height="wrap_content" android:layout_weight="1.0"  
    34.             android:text="@string/txt_btn_login" />  
    35.   
    36.         <Button android:id="@+id/btn_reset" android:layout_width="fill_parent"  
    37.             android:layout_height="wrap_content" android:layout_weight="1.0"  
    38.             android:text="@string/txt_btn_reset" />  
    39.     </LinearLayout>  
    40. </TableLayout>  

     

     好了今天暂时写到这里,明天继续service。

     

    参考资料:

    http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html

    http://www.cnblogs.com/feisky/archive/2010/01/01/1637427.html

    http://bbs.iandroid.cn/android-9510-1-1.html

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值