10.安卓基础之新特性和知识点回顾

1. 使用 ContentProvider 获得系统的联系人

ContentProvider : 后门程序,就是一个继承了ContentProvder的一个类.使用系统已经提供好的后门程序,来获得所有的其他的应用中的数据.
例如 : 获取到系统的短信内容,获取手机联系人的信息.

  • 先获取到读取通讯录权限
1
<uses-permission android:name="android.permission.READ_CONTACTS"/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//ContactsUtils 获得系统的联系人的信息
public class ContactsUtils {
public static List<ContactInfo> displayContact(Context context){
List<ContactInfo> list = new ArrayList<ContactInfo>();
Uri contact_uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri data_uri = Uri.parse("content://com.android.contacts/data");
//获得与后门程序打交道的resolver对象
ContentResolver resolver = context.getContentResolver();
Cursor contact_cursor = resolver.query(contact_uri,
new String[]{"contact_id"},null,null,null);
while(contact_cursor.moveToNext()){
String id = contact_cursor.getString(0);
if(id!=null){
ContactInfo info = new ContactInfo();
Cursor dataCursor = resolver.query(data_uri, new String[] {
"data1", "mimetype" }, "raw_contact_id=?",
new String[] { id }, null);
while(dataCursor.moveToNext()){
String data1 = dataCursor.getString(0);
String type = dataCursor.getString(1);
if ("vnd.android.cursor.item/name".equals(type)) {
info.setName(data1);
} else if ("vnd.android.cursor.item/email_v2".equals(type)) {
info.setEmail(data1);
} else if ("vnd.android.cursor.item/im".equals(type)) {
info.setQq(data1);
} else if ("vnd.android.cursor.item/phone_v2".equals(type)) {
info.setPhone(data1);
}
}
list.add(info);
dataCursor.close();
}
}
contact_cursor.close();
return list;
}
}

2. Fragment 的使用

  • layout 的写法 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
activity_main.xml
<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" >
<FrameLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:text="@string/hello_world" >
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="sound"
android:text="声音" />
<Button
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="display"
android:text="显示" />
<Button
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="storage"
android:text="存储" />
</LinearLayout>
</LinearLayout>
displayfragment.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:background="#4400ff00"
android:orientation="vertical" >
<TextView
android:text="显示的fragment内容 "
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
soundfragment.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:background="#44ff0000"
android:orientation="vertical" >
<TextView
android:text="声音的fragment内容 "
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
storagefragment.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:background="#440000ff"
android:orientation="vertical" >
<TextView
android:text="存储的fragment内容 "
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
  • fragmentquickstart的快速入门
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
MainActivity.java
@SuppressLint("NewApi")
public class MainActivity extends Activity {
FragmentManager manager;
FragmentTransaction transaction;
SoundFragment sf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
//事务
transaction = manager.beginTransaction();
sf = new SoundFragment();
transaction.replace(R.id.container, sf);
transaction.commit(); //提交事务
}
//声音的
public void sound(View v){
//在右侧的 FrameLayout去显示sound相关的fragment数据
transaction = manager.beginTransaction();
// fragment 可以直接 new 出来, 并且不需要到 清单文件中进行注册
SoundFragment sf = new SoundFragment();
//拿到一个frament的manager对象
//事务
// 表示使用 SoundFragment去替换掉之前的framelayout
transaction.replace(R.id.container, sf);
transaction.commit();
}
//显示的
public void display(View v){
transaction = manager.beginTransaction();
DisplayFragment df = new DisplayFragment();
transaction.replace(R.id.container, df);
transaction.commit();
}
//存储的
public void storage(View v){
transaction = manager.beginTransaction();
StorageFragment ssf = new StorageFragment();
transaction.replace(R.id.container, ssf);
transaction.commit();
}
}
DisplayFragment.java
@SuppressLint("NewApi")
public class DisplayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//为fragment声明layout文件,然后将layout文件的显示转换为一个view对象
return inflater.inflate(R.layout.displayfragment, null);
}
}
SoundFragment.java
@SuppressLint("NewApi")
public class SoundFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//为fragment声明layout文件,然后将layout文件的显示转换为一个 view 对象
//之前为了将layout文件转换为 view 对象时,调用的是
//View.inflate(context, resource, root)
return inflater.inflate(R.layout.soundfragment, null);
}
}
StorageFragment.java
@SuppressLint("NewApi")
public class StorageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//为fragment声明layout文件,然后将layout文件的显示转换为一个 view 对象
return inflater.inflate(R.layout.storagefragment, null);
}
}

2.1 Fragment 的声明周期

  • Oncreate : 创建的时候执行
  • Onstart : 可见的时候执行
  • Onresume : 获得焦点的时候执行
  • Onpause : 失去焦点的时候执行
  • Onstop : 不可见的时候执行
  • Onrestart : 按了 home 键,重新回到 activity 的时候执行.
  • Ondestory : activity 销毁的时候执行.