基礎day06筆記-activity組件

Android中四大組件
activity 廣播接收者(broadCastReceiver)  服務(Service) 內容提供者(contentprovider) 
四大組件都需要在清單文件裏面配置一下
1 如何創建一個新的Activity 
 
(1)如果你想讓你的Activity有多個啓動圖標 需要這樣配置
  1. <intent-filter>
  2. <action android:name="android.intent.action.MAIN" />
  3. <category android:name="android.intent.category.LAUNCHER" />
  4. </intent-filter>

(2)Activity節點下的icon和label 可以和Application節點的icon和label不一樣 



2 隱式意圖  
  通過指定一組動作或者數據 開啓activity


3 顯示意圖 
  通過指定具體包名和類名 開啓activity  

  總結 
  (1)顯示意圖更加安全一些 
  (2)開啓自己應用的界面用顯示意圖(不需要配置意圖過濾器) 
  (3)隱式意圖一般開啓系統應用(電話撥號器 短信的發送器等等)的界面

4 人品計算器小案例
  url:資源定位符   www.baidu.com
  uri:統一資源標示符 com.itheima.
 
  (1)搭建頁面 
   

    (2) 寫mainActivity的業務邏輯 
  1. public void click(View v) {
  2. //[1]獲取用戶名
  3. String name = et_name.getText().toString().trim();
  4. if (TextUtils.isEmpty(name)) {
  5. Toast.makeText(getApplicationContext(), "用戶名不能爲空", Toast.LENGTH_LONG).show();
  6. return;
  7. }
  8. //[2]判斷選中性別
  9. int checkedRadioButtonId = rg_group.getCheckedRadioButtonId();
  10. //[3]判斷一下具體選中的性別
  11. int sex = 0; //默認值爲0
  12. switch (checkedRadioButtonId) {
  13. case R.id.rb_male: //選中的是男
  14. sex = 1;
  15. break;
  16. case R.id.rb_female: //選中的是女
  17. sex = 2;
  18. break;
  19. case R.id.rb_other: //代表選中的是人妖
  20. sex = 3;
  21. break;
  22. }
  23. //[4]判斷性別
  24. if (sex == 0) {
  25. Toast.makeText(getApplicationContext(), "親 請選擇性別 ", 0).show();
  26. return;
  27. }
  28. //[5] 跳轉到resutActivity頁面 顯示意圖
  29. Intent intent = new Intent(this,ResultActivity.class);
  30. //[5.1]要把name 和 sex 傳遞到結果頁面 底層map
  31. intent.putExtra("name", name);
  32. intent.putExtra("sex", sex);
  33. //[6]開啓Activity
  34. startActivity(intent);
  35. }
   
(3)寫resutlActivity的業務邏輯
  1. super.onCreate(savedInstanceState);
  2. // 加載頁面
  3. setContentView(R.layout.activity_result);
  4. // [1]找到我們關心的控件
  5. TextView tv_name = (TextView) findViewById(R.id.tv_name);
  6. TextView tv_sex = (TextView) findViewById(R.id.tv_sex);
  7. TextView tv_result = (TextView) findViewById(R.id.tv_result);
  8. // [2]獲取開啓此Activity的意圖對象
  9. Intent intent = getIntent();
  10. // [3]獲取我們攜帶過來的數據 取出性別和name 傳遞的是什麼樣的數據類型 你在取的時候
  11. String name = intent.getStringExtra("name");// 獲取name
  12. int sex = intent.getIntExtra("sex", 0);
  13. // [4]把數據顯示到控件上
  14. tv_name.setText(name); // 顯示姓名
  15. // [5]顯示性別
  16. byte[] bytes = null;
  17. try {
  18. switch (sex) {
  19. case 1: // 代表男
  20. tv_sex.setText("男");
  21. bytes = name.getBytes("gbk");
  22. break;
  23. case 2:
  24. tv_sex.setText("女");
  25. bytes = name.getBytes("utf-8");
  26. break;
  27. case 3:
  28. tv_sex.setText("人妖");
  29. bytes= name.getBytes("iso-8859-1");
  30. break;
  31. }
  32. } catch (UnsupportedEncodingException e) {
  33. e.printStackTrace();
  34. }
  35. //[6]根據我們輸入的姓名和性別 來計算人品得分 根據得分顯示結果
  36. int total = 0;
  37. for (byte b : bytes) { //0001 1000
  38. int number =b&0xff; //1111 1111
  39. total+=number;
  40. }
  41. //算出得分
  42. int score = Math.abs(total)%100;
  43. if (score >90) {
  44. tv_result.setText("您的人品非常好 您家的祖墳都冒青煙了");
  45. }else if (score >70) {
  46. tv_result.setText("有你這樣的人品算是不錯了..");
  47. }else if (score >60) {
  48. tv_result.setText("您的人品剛剛及格");
  49. }else{
  50. tv_result.setText("您的人品不及格....");
  51. }


5 短信大全小案例
  1. //(1)獲取控件
  2. ListView lv = (ListView) findViewById(R.id.lv);
  3. //(2)lv展示數據需要數據適配器 adapter
  4. ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item, objects);
  5. //(3)設置數據適配器
  6. lv.setAdapter(adapter);
  7. //(4)設置條目的監聽
  8. lv.setOnItemClickListener(new OnItemClickListener() {
  9. //當條目被點擊的時候調用
  10. @Override
  11. public void onItemClick(AdapterView<?> parent, View view,
  12. int position, long id) {
  13. //(5) 獲取我們點擊條目的數據 小技巧 數據在哪裏存着呢 就去哪裏去
  14. String content = objects[position];
  15. System.out.println("content:"+content);
  16. //(6)跳轉到發送短信頁面 用隱式意圖
  17. /*<intent-filter>
  18. <action android:name="android.intent.action.SEND" />
  19. <category android:name="android.intent.category.DEFAULT" />
  20. <data android:mimeType="text/plain" />
  21. </intent-filter>*/
  22. //(7)創建意圖對象
  23. Intent intent = new Intent();
  24. intent.setAction("android.intent.action.SEND");
  25. intent.addCategory("android.intent.category.DEFAULT");
  26. intent.setType("text/plain");
  27. //(7.1)把數據傳遞到 短信的發送頁面
  28. intent.putExtra("sms_body", content); //要求大家知道這個是怎麼來的 ??
  29. //(8)開啓意圖
  30. startActivity(intent);
  31. }
  32. });
  33. }



6 短信發送器小案例  smsManager
   4 5案例有一個共同的特點:A 界面開啓B界面 然後把A界面裏面的數據傳遞B界面 
   A ---->B   當B界面關閉的時候把數據回傳給A界面 
   
 總結實現步驟 
  (1)畫 mainActivity頁面 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
 
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
 
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入要發送的號碼 " />
 
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/et_number"
android:text="+" />
</RelativeLayout>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="250dp"
android:gravity="top"
android:hint="請輸入要發送的內容 " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="insert"
android:text="插入短信模板" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="發送" />
 
</LinearLayout>
 (2) 畫聯繫人頁面  用一個listview展示就可以了  
  (3)寫mainActivity的邏輯    主要代碼 
   
package com.itheima.smssender;
 
public class MainActivity extends Activity {
 
private EditText et_number;
private EditText et_content;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//顯示短信發送的內容
et_content = (EditText) findViewById(R.id.et_content);
//顯示號碼
et_number = (EditText) findViewById(R.id.et_number);
 
}
 
//點擊按鈕 跳轉到聯繫人頁面
public void click(View v){
//創建一個意圖對象 跳轉聯繫人頁面
Intent intent = new Intent(this,ContactActivity.class);
//如果你調用這個方法 就是簡單開啓頁面 不能拿到返回的數據 注意:如果你想開啓一個activity 並且想要獲取開啓的這個activity返回的數據 要調用這個方法
startActivityForResult(intent, 1);
// startActivity(intent);
}
//點擊按鈕 實現發送短信短信的邏輯
public void send(View v){
//TODO 一會實現
}
//點擊按鈕 打開插入短信模板 頁面
public void insert(View v){
Intent intent = new Intent(this,SmsTemplateActivity.class);
startActivityForResult(intent, 2);
}
 
//當這個activity(MainActivity) 開啓聯繫人頁面時 當聯繫人頁面關閉的時候這個方法會調用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("onActivityResult");
/*if (resultCode == 10) {
//代表這個數據 是從聯繫人頁面獲取的
//(1)取出我們在聯繫人頁面設置的數據
String phone = data.getStringExtra("phone");
et_number.setText(phone);
}else if (resultCode == 20) {
//(2)把短信模板頁面的數據顯示到et_content控件上
String smscontent = data.getStringExtra("smscontent");
et_content.setText(smscontent);
}
*/
if (requestCode == 1) {
//代表請求聯繫人頁面
//(1)取出我們在聯繫人頁面設置的數據
String phone = data.getStringExtra("phone");
et_number.setText(phone);
}else if(requestCode == 2){
//請求短信模板頁面
//(2)把短信模板頁面的數據顯示到et_content控件上
String smscontent = data.getStringExtra("smscontent");
et_content.setText(smscontent);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
(4)寫短信模板頁面  也是用一個listview展示即可  
(5)短信模板頁面主要代碼 如下 
       
package com.itheima.smssender;
 
public class SmsTemplateActivity extends Activity {
 
String objects[] = {"我在開會,請稍後在打...","我在喫飯,請稍後在打...","我在打醬油,請稍後在打...","我在約會,請稍後在打...","我在打麻將,請稍後在打..."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加載這個佈局
setContentView(R.layout.activity_smstemplate);
//(1)找到listview
ListView lv = (ListView) findViewById(R.id.lv);
//(2)把我們定義的好數據顯示到listview上
lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.smstemlpate_item, objects));
//(3)給listview設置點擊事件
lv.setOnItemClickListener(new OnItemClickListener() {
 
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//(4)取出點擊條目的數據
String smscontent = objects[position];
//(5)當頁面關閉的時候把數據返回
Intent intent = new Intent();
intent.putExtra("smscontent", smscontent);
//(6)把數據返回給調用者
setResult(20, intent);
//(7)關閉這個Activity
finish();
}
});
}
}

(6)實現發送短信的功能 
      
//(2)實現發送短信的功能 smsManager 獲取這個類的實例
SmsManager smsManager = SmsManager.getDefault();
//(3)分割短信 分條發送
ArrayList<String> divideMessages = smsManager.divideMessage(content);
for (String div : divideMessages) {
//(4)發送短信
smsManager.sendTextMessage(number, null, div, null, null);
}
(7)記得要加上發送短信的權限
android.permission.SEND_SMS

7 Activity的生命週期 (必須掌握)
  oncreate 方法 當Activity第一次啓動的時候調用
  onDestroy 方法 當Activity銷燬的時候調用
  onStrat() 方法 當Activity變成可見的時候調用 
  onStop() 方法 當activity 不可見的時候調用
  onResume()方法 當activity可以獲取焦點的時候  當界面的按鈕可以被點擊了
  onPause()方法 當失去焦點的時候調用 當按鈕不了可以被點擊的時候調用
  onRestart()當界面重新啓動的時候調用

8 橫豎屏切換Activity的生命週期
 (1)爲了防止橫豎屏切換 生命週期會發生變化 所以把Activity配置如下 
  android:screenOrientation="portrait"
  (2)爲了聲明週期不變化 
 android:configChanges="orientation|keyboardHidden|screenSize" 

  

9 任務棧的概念
  棧:先進後出 
  隊列:先進先出 
  Task 打開一個Activity叫進棧  關閉一個activit出棧   
  任務棧是用來維護Activity的 是用來維護用戶的操作體驗
  我們操作的Activity永遠是任務棧的棧頂的Activity
  說應用程序退出了 實際上任務棧清空了


 10 Activity的四種啓動模式
     ##12_singletop啓動模式
  1.     singletop 單一頂部模式 在activity的配置文件中設置android:launchMode="singleTop"
        如果任務棧的棧頂存在這個要開啓的activity,不會重新的創建activity,而是複用已經存在的activity。保證棧頂如果存在,不會重複創建。
        應用場景:瀏覽器的書籤

        
    ##13_singletask和singleinstance啓動模式
        singetask 單一任務棧,在當前任務棧裏面只能有一個實例存在
        當開啓activity的時候,就去檢查在任務棧裏面是否有實例已經存在,如果有實例存在就複用這個已經存在的activity,並且把這個activity上面的所有的別的activity都清空,複用這個已經存在的activity。保證整個任務棧裏面只有一個實例存在
        
        應用場景:瀏覽器的activity
        如果一個activity的創建需要佔用大量的系統資源(cpu,內存)一般配置這個activity爲singletask的啓動模式。

        singleInstance啓動模式非常特殊, activity會運行在自己的任務棧裏面,並且這個任務棧裏面只有一個實例存在
        如果你要保證一個activity在整個手機操作系統裏面只有一個實例存在,使用singleInstance
        應用場景: 來電頁面 
  有道詞典



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章