1.activity的生命周期
这七个方法定义了Activity的完整生命周期。实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环:
(1)Activity的完整生命周期
自第一次调用onCreate()开始,直到调用onDestory()为止。Activity在onCreate()中设置所有“全局”状态以完成初始化。
而在onDestory()中释放所有系统资源。例如,如果Activity有一个线程在后台运行从网络下载数据,它会在onCreate()创建线程,
而在onDestory()销毁线程
(2)Activity的完整可视周期
自onStart()调用开始直到相应的onStop()调用结束。在此期间,用户可以在屏幕上看到Activity,尽管它也许并不是位于前台
或者也不与用户进行交互。在这两个方法之间,我们可以保留用来向用户显示这个Activity所需的资源。例如,当用户不再看见
我们显示的内容时,我们可以在onStart()中注册一个BroadcastReceiver来监控会影响UI的变化,而在onStop()中来注销。onStart()
和onStop()方法可以随着应用程序是否为用户可见而被多次调用。
(3)Activity的前台生命周期
自onResume()调用起,至相应的onPause()调用为止。在此期间,Activity位于前台最上面并于用户进行交互。
Activity会经常在暂停和恢复之间进行状态转换。例如当设备转入休眠状态或者有新的Activity启动时,将调用
onPause()方法。当Activity获得结果或者接收到新的Intent时会调用onResume()方法。
2.activity的启动模式
standard: 默认每次激活一个那么任务栈就放置一个新的Activity实例。默认是标准模式。
singleTop:栈顶只能保证有一个该Activity的实例。
singleTask: 在当前任务栈中只能有一个实例,如果添加之前任务栈中已经有了该实例,那么已有的该实例以上的实例全部出栈,显示当前的实例。
singleTop:保证多个任务栈中只能有一个实例。
使用场景:
standard 即将每次激活的Activity放入到任务栈栈顶。(一般不用,除非你不激活Activity)
singleTop即每次进栈之前先判断栈顶是否有该Activity,有则不进,没有则进。但是依旧执行onNewIntent()方法。如:书签
singleTask即保证任务栈中该Activity是单例,Activity01àActivity02àActivity01如果
的Activity01被杀死。如:截屏
singleInstance 该启动模式会单独的形成一个任务栈存储激活的Activity,让其他应用共享该
Activity。如:其他应用直接使用打开的浏览器。
3.activity横竖屏切换的生命周期
.开启新的activity获取他的返回值
(1)三个布局文件
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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/et_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:hint="请输入联系人号码"
android:inputType="text" >
</EditText>
<Button
android:layout_alignBottom="@+id/et_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="selectContacts"
android:text="选择联系人" />
</RelativeLayout>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:minLines="10" >
<requestFocus />
</EditText>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="send"
android:text="发送" />
</LinearLayout>
activity_contacts.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" >
<ListView
android:id="@+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
contact_item.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" >
<TextView
android:layout_marginTop="5dp"
android:id="@+id/tv_name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="25sp" />
<TextView
android:layout_marginTop="5dp"
android:id="@+id/tv_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="号码"
android:textSize="25sp" />
</LinearLayout>
(2)2个Activity
public class MainActivity extends Activity {
private EditText et_contact;
private EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_contact = (EditText) this.findViewById(R.id.et_contact);
this.et_content = (EditText) this.findViewById(R.id.et_content);
}
public void selectContacts(View view) {
Intent intent = new Intent(this, ContactActivity.class);
this.startActivityForResult(intent, 0);
}
public void send(View view) {
String number = this.et_contact.getText().toString().trim();
String content = this.et_content.getText().toString();
if (TextUtils.isEmpty(content)) {
Toast.makeText(this, "内容不能为空!", 0).show();
return;
}
if (TextUtils.isEmpty(number)) {
Toast.makeText(this, "号码不能为空!", 0).show();
return;
}
sendSms(number, content);
ContentResolver resolver = this.getContentResolver();
insertSms(number, content, resolver);
Toast.makeText(this, "短信已发送!", 0).show();
}
private void insertSms(String number, String content,
ContentResolver resolver) {
Uri uri = Uri.parse("content://sms");
ContentValues values = new ContentValues();
values.put("address", number);
values.put("type", 2);
values.put("date", System.currentTimeMillis());
values.put("body", content);
resolver.insert(uri, values);
}
private void sendSms(String number, String content) {
SmsManager manager = SmsManager.getDefault();
ArrayList<String> contents = manager.divideMessage(content);
for (String str : contents) {
manager.sendTextMessage(number, null, str, null, null);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
String number = data.getStringExtra("number");
et_contact.setText(number);
}
}
}
public class ContactActivity extends Activity {
private ArrayList<HashMap<String, String>> smsInfos = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
ListView lv_contacts = (ListView) this.findViewById(R.id.lv_contacts);
smsInfos = this.getContacts();
lv_contacts.setAdapter(new SimpleAdapter(this, smsInfos,
R.layout.contact_item, new String[] { "name", "number" },
new int[] { R.id.tv_name, R.id.tv_number }));
lv_contacts.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap<String, String> smsInfo = smsInfos.get(arg2);
String number = smsInfo.get("number");
Intent intent = new Intent();
intent.putExtra("number", number);
setResult(0, intent);
finish();
}
});
}
private ArrayList<HashMap<String, String>> getContacts() {
ArrayList<HashMap<String, String>> smsInfos = new ArrayList<HashMap<String, String>>();
HashMap<String, String> smsInfo = null;
ContentResolver resolver = this.getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri dataUri = Uri.parse("content://com.android.contacts/data");
Cursor cursor = resolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("contact_id"));
if (id != null) {
smsInfo = new HashMap<String, String>();
Cursor dataCursor = resolver.query(dataUri, new String[] {
"data1", "mimetype" }, "raw_contact_id=?",
new String[] { id }, null);
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if ("vnd.android.cursor.item/name".equals(mimetype)) {
smsInfo.put("name", data1);
}
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
smsInfo.put("number", data1);
}
}
smsInfos.add(smsInfo);
dataCursor.close();
}
}
cursor.close();
return smsInfos;
}
}
(3)授权
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
5.利用广播实现ip拨号
<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" >
<EditText
android:id="@+id/et_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存" />
</LinearLayout>
public class MainActivity extends Activity {
private EditText et_ip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_ip = (EditText) this.findViewById(R.id.et_ip);
SharedPreferences sf = this
.getSharedPreferences("config", MODE_PRIVATE);
this.et_ip.setText(sf.getString("ip", ""));
}
public void save(View view) {
String ip = this.et_ip.getText().toString().trim();
SharedPreferences sf = this
.getSharedPreferences("config", MODE_PRIVATE);
Editor edit = sf.edit();
edit.putString("ip", ip);
edit.commit();
Toast.makeText(this, "保存完毕!", 0).show();
}
}
自定义一个广播
public class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "电话通了。。。。", 0).show();
System.out.println("电话通了。。。。");
String number = getResultData();
SharedPreferences sf = arg0.getSharedPreferences("config",
Context.MODE_PRIVATE);
String ip = sf.getString("ip", "");
String newNumber = ip + number;
setResultData(newNumber);
}
}
<receiver android:name=".OutCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" >
</action>
</intent-filter>
</receiver>
6.短信接收广播
public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "接收到新短信!", 1).show();
}
}
<activity
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name="com.itheima.smsreciver.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>
<receiver android:name=".SmsReceiver">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
7.短信监听器
1.自定义广播类
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for (Object pdu : pdus) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
String body = message.getMessageBody();
String sender = message.getOriginatingAddress();
System.out.println("body = " + body);
System.out.println("sender = " + sender);
if ("5556".equals(sender)) {
// 终止广播
abortBroadcast();
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(sender, null, "i hite you", null, null);
}
}
}
}
2.授权
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<receiver android:name=".SmsListener">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>