【0045】Android基础-33-Activity相关

【1】如何创建一个新的Activity

【总结】

【1】页面布局:养成良好的习惯,在创建了四大组件之后就随手在清单文件(AndroidManifest.xml)中进行配置:

【2】重点内容:显示调用和隐式调用、两个页面之间传递数据、开启Activity的两种方式、请求码和结果的作用、Activity的生命周期

 

【1.1】清单的配置

【1.1.1】清单的配置:activity中的icon属性和label属性

 

【1.1.2】清单的配置:Application中的label属性

【1.1.3】清单配置一个应用程序有多个启动图标

【1.2】页面之间的跳转

  一个程序中只能有一个启动图标,可以从一个启动的页面Activity跳转到另外一个Acitivity界面

【1.2.1】第一个页面中的拨号按钮跳转到打电话的页面

【原理】因为在打电话的应用程序的清单中存在<data android:sheme="tel"> 中data字段中含有tel字段

查看源码:

 

【1.2.2】第一个页面中的跳转按钮跳转到第二个页面

【第一种方法】

【第二种方法】

【第三种方法】:增加别的字段进行配置

  理论上只要是在清单上出现的Acitvity中的<intent-filter>中数据都应该在源码中进行配置

【问题出现的原因】data 和type字段的内容会互相清除

 

 【解决办法】两者需要同时使用另外的一种方法

 

【2】logcat不打印日志的调试方法:①reset adb ②重新开启模拟器

【3】隐式意图:在清单中存在多个意图过滤,只要在源码匹配中匹配一个完整的字段就可以;没有特别指明

 

源码匹配中只要匹配其中的一个选项就可以;

同样可以跳转到第二个界面

【4】显示意图

【新建第二个测试的Activity】其中的intent_filter不需要配置

【第一种方法】参数添加的是设置的要跳转的xml文件对应的class文件

【第二种方法】

 

【5】总结:显示意图和隐式意图使用的场合

【x】快捷键-模板的定义

【6】人品计算器小实例

【6.1】页面布局

【6.2】对姓名和性别的选择不为空进行判断、页面跳转功能的完成

【源码】

【layout/activity_main.xml】第一个主页面的布局

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:orientation="vertical"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:paddingTop="20dp"
 8     tools:context="com.example.administrator.a05persontest.MainActivity">
 9 
10     <EditText
11         android:id="@+id/et_inputName"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:hint="请输入你的名字"/>
15 
16     <RadioGroup
17         android:id="@+id/rg_group"
18         android:orientation="horizontal"
19         android:gravity="center"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content">
22         <RadioButton
23             android:id="@+id/male"
24             android:layout_weight="1"
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:text="男"/>
28         <RadioButton
29             android:id="@+id/female"
30             android:layout_weight="1"
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:text="女"/>
34         <RadioButton
35             android:id="@+id/other"
36             android:layout_weight="1"
37             android:layout_marginRight="10dp"
38             android:layout_width="wrap_content"
39             android:layout_height="wrap_content"
40             android:text="人妖"/>
41 
42 
43     </RadioGroup>
44     <Button
45         android:id="@+id/bt_calc"
46         android:layout_width="match_parent"
47         android:layout_height="wrap_content"
48         android:text="计算"/>
49 
50 </LinearLayout>

【layout/activity_test.xml】第二个页面的布局

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <TextView
 8         android:id="@+id/tv_name"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="3333333333333333" />
12 
13     <TextView
14         android:id="@+id/tv_sex"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="22222222222222222222"/>
18 
19     <TextView
20         android:id="@+id/tv_calcResult"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="111111111111111111111"/>
24 
25 
26 
27 </LinearLayout>

 【com.example.administrator.a05persontest.MainActivity】源码

 1 package com.example.administrator.a05persontest;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.text.TextUtils;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.RadioGroup;
12 import android.widget.Toast;
13 
14 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
15 
16     private EditText et_inputName;
17     private RadioGroup rg_group;
18     private Context mContext;
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23 
24         //1.找到控件
25         et_inputName = (EditText) findViewById(R.id.et_inputName);
26         rg_group = (RadioGroup) findViewById(R.id.rg_group);
27         Button bt_calc = (Button) findViewById(R.id.bt_calc);
28         bt_calc.setOnClickListener(this);
29 
30 
31 
32     }
33 
34     @Override
35     public void onClick(View view) {
36         //2.判断输入的姓名不能为空
37         String name = et_inputName.getText().toString().trim();
38         if (TextUtils.isEmpty(name)){
39             Toast.makeText(getApplicationContext(),"亲 请输入你的姓名",Toast.LENGTH_SHORT).show();
40             return;
41         }
42 
43         //3.性别必须选
44         int sexVal = 0;
45         switch (rg_group.getCheckedRadioButtonId()){
46             case R.id.male:
47                 sexVal = 1;
48                 break;
49             case R.id.female:
50                 sexVal = 2;
51                 break;
52             case R.id.other:
53                 sexVal = 3;
54                 break;
55             default:
56                 break;
57         }
58         if (0 == sexVal){
59             Toast.makeText(getApplicationContext(),"请选择性别",Toast.LENGTH_SHORT).show();
60             return;
61         }
62 
63         Intent intent = new Intent(this, CalcTestActivity.class);
64         startActivity(intent);
65     }
66 
67 
68 }

 【app/src/main/AndroidManifest.xml】配置

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.a05persontest">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android:theme="@style/AppTheme">
12         <activity android:name=".MainActivity">
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <activity android:name="com.example.administrator.a05persontest.CalcTestActivity"></activity>
20 
21     </application>
22 
23 </manifest>

【6.3】跳转页面携带第一个页面中的姓名和性别的数据 

 putExtra()方法可以传递很多类型的数据,包括八大数据类型等等;

【第一个页面传递数据】使用intent意图传递数据

【第二个页面接收数据】通过intent接收数据( getIntent()),接收的数据类型和传递的数据类型要一致;

 1 package com.itheima.rpcalc;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.TextView;
10 
11 public class ResultActivity extends Activity {
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         // 加载页面
17         setContentView(R.layout.activity_result);
18         // [1]找到我们关心的控件
19         TextView tv_name = (TextView) findViewById(R.id.tv_name);
20         TextView tv_sex = (TextView) findViewById(R.id.tv_sex);
21         TextView tv_result = (TextView) findViewById(R.id.tv_result);
22 
23         // [2]获取开启此Activity的意图对象
24         Intent intent = getIntent();
25         // [3]获取我们携带过来的数据 取出性别和name 传递的是什么样的数据类型 你在取的时候
26         String name = intent.getStringExtra("name");// 获取name
27         int sex = intent.getIntExtra("sex", 0);
28 
29         // [4]把数据显示到控件上
30         tv_name.setText(name); // 显示姓名
31 
32         // [5]显示性别
33         byte[] bytes = null;
34         
35         try {
36             switch (sex) {
37             case 1: // 代表男
38                 tv_sex.setText("男");
39                 bytes = name.getBytes("gbk");
40                 
41                 break;
42 
43             case 2:
44                 tv_sex.setText("女");
45                 bytes = name.getBytes("utf-8");
46                 break;
47 
48             case 3:
49                 tv_sex.setText("人妖");
50                 bytes= name.getBytes("iso-8859-1");
51                 break;
52             }
53         } catch (UnsupportedEncodingException e) {
54             e.printStackTrace();
55         }
56 
57         //[6]根据我们输入的姓名和性别 来计算人品得分  根据得分显示结果  
58         int  total = 0;
59         for (byte b : bytes) {     //0001 1000
60               int number =b&0xff;             //1111 1111
61               total+=number;
62         }
63         
64         //算出得分 
65         int score = Math.abs(total)%100;
66         if (score >90) {
67             tv_result.setText("您的人品非常好 您家的祖坟都冒青烟了");
68         }else if (score >70) {
69             tv_result.setText("有你这样的人品算是不错了..");
70         }else if (score >60) {
71             tv_result.setText("您的人品刚刚及格");
72         }else{
73             tv_result.setText("您的人品不及格....");
74             
75         }
76 
77     }
78 
79 }

【6.4】出现的问题:直接将int型数据不能传递给TextView;需要转化为String类型;

1 10-27 02:21:49.792: E/AndroidRuntime(9001): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1

 

 

【解决方法】

【7】短信大全小案例

【功能】点击应用中的某个条目之后自动转到短信发送的界面,条目的内容直接复制到短信要发送的内容框中;

【7.1】布局及信息内容的展示

【activity_main.xml】

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <ListView
 8         android:id="@+id/lv"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" >
11     </ListView>
12 
13 </RelativeLayout>

【layout/item.xml】

1 <?xml version="1.0" encoding="utf-8"?>
2 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
3     android:id="@+id/textView1"
4     android:layout_width="wrap_content"
5     android:layout_height="wrap_content"
6     android:text="TextView"
7     android:textColor="#000000"
8     android:textAppearance="?android:attr/textAppearanceLarge" />

 

 

【7.2】点击条目之后信息的跳转到短信的内容框

【小技巧1】

【小技巧2】查看函数的参数的小技巧

【小技巧3】

 

 

 【源码】

【7.3】点击条目之后信息的跳转到短信的内容框之后将数据传递给短信输入框

通过在源码中搜索关键字知道该选择哪个“key”;

以为传递的是String类型,并且是获取“get”,其中最后包含了(put)Extra,因此选择搜索的关键字是:“getStringExtra”

 

 

  

【7.4】短信大全所有的源码


 1 package smscollection.com.example.a06smscollection;
 2 
 3 import android.content.Intent;
 4 import android.net.PskKeyManager;
 5 import android.net.Uri;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.AdapterView;
10 import android.widget.ArrayAdapter;
11 import android.widget.ListView;
12 import android.widget.TextView;
13 
14 public class MainActivity extends AppCompatActivity {
15 
16     private ListView lv_sms;
17     private TextView tv;
18 
19     String[] objects = {
20             "我的祝福最霸道,收到你就没烦恼,白天黑夜乐淘淘;我的短信最美好,看后愁闷都得跑,轻松愉快才逍遥;我的信息最牢靠,幸福吉祥来笼罩,美梦成真在今朝;我的祝愿最着调,生日当天有爆料,欢歌笑语总围绕!",
21             "你在我的心中扎根,我们的友谊露重更深;你在我的牵挂中缤纷,我们的情感喜大普奔;你在我的坦诚中光临,我们的心有灵犀更近;你在我的问候中欢欣,我们的关爱情不自禁;你在我的祝福中兴奋,祝你生日快乐开心!",
22             "好友如梦似幻,日夜兼程思念,亲朋血脉相连,话语滋润心田,知音一生有缘,牵手幸福永远,挚爱看雨听蝉,开心快乐无边,知己晓得冷暖,经常相互惦念,我心与你相牵,送你灿烂心愿,祝你生日圆满,谱写壮丽诗篇!"
23     };
24 
25 
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_main);
30 
31         //1.先找到listview控件
32         lv_sms = (ListView) findViewById(R.id.lv_sms);
33 
34         //2.设置listView的点击事件
35 //        lv_sms.setOnClickListener((View.OnClickListener) this);
36 
37         //2.将数据内容显示到listView中
38 //        public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<T> objects)
39 //        resource:负责展示数据的layout文件
40         tv = (TextView) findViewById(R.id.tv);
41         ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_item, objects);
42         lv_sms.setAdapter(stringArrayAdapter);
43         //3.设置点击事件
44         lv_sms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
45             @Override
46             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
47                 //3.1 获取短信的内容
48                 String content = objects[position];
49 
50                 //3.2 点击该项目之后跳转到SMS界面
51 //        <intent-filter>
52 //               <action android:name="android.intent.action.SEND" />
53 //               <category android:name="android.intent.category.DEFAULT" />
54 //               <data android:mimeType="text/plain" />
55 //           </intent-filter>
56 
57                 //跳转到短信输入框
58                 Intent intent = new Intent();
59                 intent.setAction("android.intent.action.SEND");
60                 intent.addCategory("android.intent.category.DEFAULT");
61                 intent.setType("text/plain");
62                 //跳转到输入联系人
63 //                Uri smsToUri = Uri.parse("smsto:");
64 //                Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
65 
66                 intent.putExtra("sms_body", content);
67                 //4.开启意图
68                 startActivity(intent);
69 
70             }
71         });
72 
73     }
74 
75 }

 

【8】短信发送器案例:自己实现一个短信发送的应用,需要使用到smsManager类

 上面的案例有一个共同的特点:A 界面开启B界面 然后把A界面里面的数据传递B界面 
   下面的案例要求:A ---->B   当B界面关闭的时候把数据回传给A界面 

【8.1】页面布局:养成良好的习惯,在创建了四大组件之后就随手在清单文件(AndroidManifest.xml)中进行配置:

【activity_main.xml】源码:第一个界面的布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <RelativeLayout
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content" >
11 
12         <EditText
13             android:id="@+id/et_number"
14             android:layout_width="match_parent"
15             android:layout_height="wrap_content"
16             android:hint="请输入联系人好码" />
17 
18         <Button
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"
21             android:layout_alignBottom="@id/et_number"
22             android:layout_alignParentRight="true"
23             android:onClick="add"
24             android:text="+" />
25     </RelativeLayout>
26 
27     <EditText
28         android:id="@+id/et_sms_content"
29         android:layout_width="match_parent"
30         android:layout_height="250dp"
31         android:gravity="top"
32         android:hint="请输入发送短信的内容" />
33 
34     
35     <Button
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content"
38         android:onClick="insert"
39         android:text="插入短信模板" />
40     
41     <Button
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:onClick="click"
45         android:text="发送短信" />
46 
47 </LinearLayout>

 

【activity_contact.xml】源码:第二个界面的布局

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ListView
 8         android:id="@+id/lv_contact"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" >
11     </ListView>
12 
13 </LinearLayout>

【contact_item.xml】源码 :显示联系人的单个布局

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6 
 7     <TextView
 8         android:id="@+id/tv_name"
 9         android:layout_width="0dp"
10         android:layout_height="wrap_content"
11         android:layout_weight="1"
12         android:textColor="#000"
13         android:textSize="25sp"
14         android:text="zhangsan" />
15 
16     <TextView
17         android:id="@+id/tv_phone"
18         android:layout_width="0dp"
19         android:layout_height="wrap_content"
20         android:layout_weight="1"
21           android:textColor="#000"
22          android:textSize="25sp"
23         android:text="110" />
24 
25 </LinearLayout>

【8.2】点击“+”号之后跳转到联系人的页面,在联系人的页面显示虚拟的联系人数据

【MainActivity】源码

 1 package com.itheima.customsms;
 2 
 3 import java.util.ArrayList;
 4 
 5 import android.os.Bundle;
 6 import android.app.Activity;
 7 import android.content.Intent;
 8 import android.telephony.SmsManager;
 9 import android.view.Menu;
10 import android.view.View;
11 import android.widget.EditText;
12 
13 public class MainActivity extends Activity {
14 
15     private EditText et_number;
16     private EditText et_sms_content;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         //[1]获取我们关心的控件 
23         
24         et_number = (EditText) findViewById(R.id.et_number);
25         et_sms_content = (EditText) findViewById(R.id.et_sms_content);
26         
27         
28     }
29 
43     //点击+ 按钮 跳转到 联系人页面 
44     public void add(View v) {
45         //[1]创建意图对象  
46         Intent intent = new Intent(this,ContactActivity.class);
47         //[2]开启Activity
48 //        startActivity(intent);
49         //[3]小细节 ☆☆☆☆  如果一个页面开启另外一个页面  并且当开启的这个页面关闭的时候 还要另外一个页面的数据  使用下面这个方法开启Activity
50         startActivityForResult(intent, 1);
51         
52         
53     }   
73     }
74 }

 

【ContactActivity.java】源码

 1 package com.itheima.customsms;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.content.Intent;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.AdapterView;
12 import android.widget.AdapterView.OnItemClickListener;
13 import android.widget.BaseAdapter;
14 import android.widget.ListView;
15 import android.widget.TextView;
16 
17 public class ContactActivity extends Activity {
18 
19     private List<Contact> contactLists;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         
25         //加载布局
26         setContentView(R.layout.activity_contact);
27         //[1]找到lv控件 
28         ListView lv_contact = (ListView) findViewById(R.id.lv_contact);
29         //[2]我想把手机当中 联系人的数据展示到listview上  等讲内容提供者在去获取真实数据  
30         
31         contactLists = new ArrayList<Contact>();  
32         for (int i = 0; i < 10; i++) {
33             
34             Contact contact = new Contact();
35             contact.setName("zhangsan"+i);
36             contact.setPhone("1388900"+i);
37             contactLists.add(contact);
38             
39         }
40         //[3]展示数据  设置数据适配器 
41         lv_contact.setAdapter(new MyAdapter());
42 
43     }
44     
45     
46     //创建数据适配器 
47     private class MyAdapter extends BaseAdapter{
48 
49         @Override
50         public int getCount() {
51             return contactLists.size();
52         }
53 
54         @Override
55         public Object getItem(int position) {
56             return null;
57         }
58 
59         @Override
60         public long getItemId(int position) {
61             return 0;
62         }
63 
64         @Override
65         public View getView(int position, View convertView, ViewGroup parent) {
66             
67             View  view;
68             if (convertView == null) {
69                 view = View.inflate(getApplicationContext(), R.layout.contact_item, null);
70             }else {
71                 //复用历史缓存对象  
72                 view = convertView;
73                 
74             }
75             //找到我们关心控件 
76             TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
77             TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
78             //设置数据 
79             tv_name.setText(contactLists.get(position).getName());
80             tv_phone.setText(contactLists.get(position).getPhone());
81             
82             return view;
83         }
84         
85     }
86 
87 }

【配置清单】源码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.itheima.customsms"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10     <uses-permission android:name="android.permission.SEND_SMS"/>
11 
12     <application
13         android:allowBackup="true"
14         android:icon="@drawable/ic_launcher"
15         android:label="@string/app_name"
16         android:theme="@style/AppTheme" >
17         <activity
18             android:name="com.itheima.customsms.MainActivity"
19             android:label="@string/app_name" >
20             <intent-filter>
21                 <action android:name="android.intent.action.MAIN" />
22 
23                 <category android:name="android.intent.category.LAUNCHER" />
24             </intent-filter>
25         </activity>
26         <!--四大组件要在清单文件里面配置  -->
27         <activity android:name="com.itheima.customsms.ContactActivity"></activity>
28         
29         
30     </application>
31 
32 </manifest>

【8.3】点击第二个页面中的联系人之后将该联系人的数据返回给第一个页面输入联系人的框中

【8.3.1】第二个界面消失的逻辑

【说明】该方法的调用会导致调用该界面(第一个界面)的onActivityResult()被调用;

【8.3.2】第二个界面的数据返回给第一个界面(关键)

上面的方法执行,开启第二个页面,如果需要第二个页面返回数据则需要使用startActivityForResult()方法;

接着进入到第二个页面

 

 在第二个页面点击联系人后,获取其中的phone数据,并且设置setResult,调用finish()关闭第二个界面;再次返回第一个界面;

返回第一个界面的时候onActivityResult()方法被调用,取出第一个界面返回的结果;

 

【8.4】增加的逻辑-增加版本信息

点击模板按钮后将版本信息的内容返回给发短信的框中

【8.5】遇到的程序的bug

 

【8.6】最终实现的效果

【8.7】请求码/结果码的使用:两种都可以,主要的功能就是判断码取出返回的数据;

【结果码的使用】

【请求码的使用】本页面请求使用已经提前设置好(在startActivityForResult中设置)请求码的页面

【8.6】发送短信功能的实现

【8.6.1】smsManager类

 

【8.6.2】bug出现,需要发短信权限的设置

【8.6.3】单条发送的效果演示

【8.6.4】发送长度的限制

 

 

【9】Activity的生命周期

【9.1 】生命周期的认识

Table 1. A summary of the activity lifecycle's callback methods.

 

MethodDescriptionKillable after?Next
onCreate() Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (see Saving Activity State, later).

Always followed by onStart().

No onStart()
     onRestart() Called after the activity has been stopped, just prior to it being started again.

Always followed by onStart()

No onStart()
onStart() Called just before the activity becomes visible to the user.

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

No onResume()
or
onStop()
     onResume() Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

Always followed by onPause().

No onPause()
onPause() Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.

Yes onResume()
or
onStop()
onStop() Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

Yes onRestart()
or
onDestroy()
onDestroy() Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. Yes nothing

 

    

【9.2】程序在部署成功之后调用了三个方法

【9.3】此时应用5的button不可以再被点击,被透明应用遮挡住了,因此onPause()方法被调用

【9.4】当透明应用6退出后,应用5的button可以被再次点击,调用了onResume方法(焦点再现),但是没有调用onStart方法,因为

应用本身一直可见;

【9.5】当程序完全退出的时候方法的调用:Button焦点失去(onPause),程序不可见(onStop),程序销毁(onDestory)

【9.6】当程序从后天返回到前台时,程序再次启动(onRestart),焦点可见(onesume),页面重新可见(onStrart)

【9.7】两个页面跳转

【跳转到第二个页面】

【跳回到第一个页面】

【注意】发现在restart()方法调用的时候,start()方法一定会被调用,因此,在实际开发中会重写start()方法,而不会重写onRestart()方法;

【9.8】横竖屏切换的生命周期的调用:会先销毁再创建

【注意】因此在市面上的手机就指定好屏幕的参数不可以横竖屏切换,共有两种方法

【9.9】横竖屏写死的方法

【方法1】在公司中这种方式使用的居多

【方法2】

【10】任务栈

 

【11】Activity的四种启动模式

 

 

【11.1】standrd启动模式:默认使用该模式

 

【11.2】singletop启动模式

    singletop 单一顶部模式 在activity的配置文件中设置android:launchMode="singleTop"
    如果任务栈的栈顶存在这个要开启的activity,不会重新的创建activity,而是复用已经存在的activity。保证栈顶如果存在,不会重复创建。

    应用场景:浏览器的书签

【11.3】singleTask:在任务栈中只启动一个配置了该选项的activity实例;只复用该实例;

singetask 单一任务栈,在当前任务栈里面只能有一个实例存在
    当开启activity的时候,就去检查在任务栈里面是否有实例已经存在,如果有实例存在就复用这个已经存在的activity,并且把这个activity上面的所有的别的activity都清空,复用这个已经存在的activity。保证整个任务栈里面只有一个实例存在
    应用场景:浏览器的activity
    如果一个activity的创建需要占用大量的系统资源(cpu,内存)一般配置这个activity为singletask的启动模式。

【11.4】 singleInstance启动模式非常特殊, activity会运行在自己的任务栈里面,并且这个任务栈里面只有一个实例存在

    如果你要保证一个activity在整个手机操作系统里面只有一个实例存在,使用singleInstance
    应用场景: 来电页面 

 

posted @ 2017-10-26 16:19  OzTaking  阅读(399)  评论(0)    收藏  举报