1.serResult
package cn.itcast.getdata;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class DemoActivity extends Activity {
EditText et, et2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) this.findViewById(R.id.et_contact1);
et2 = (EditText) this.findViewById(R.id.et_contact2);
}
public void click(View view) {
Intent intent = new Intent(this, ContactActivity.class);
// startActivity(intent);
startActivityForResult(intent, 1);
}
public void click2(View view) {
Intent intent = new Intent(this, ContactActivity.class);
// startActivity(intent);
startActivityForResult(intent, 2);
}
/**
* 当被激活的activity 使用完毕 销毁的时候 就会执行onActivityresult的方法
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
System.out.println("结果码为"+ resultCode);
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
String name = data.getStringExtra("name");
if (requestCode == 1) {
et.setText(name);
}else if(requestCode == 2){
et2.setText(name);
}
}
}
}
package cn.itcast.getdata;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;a
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ContactActivity extends Activity {
ListView lv;
List<String> names;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
lv = (ListView) this.findViewById(R.id.lv);
names = getContacts();
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) view;
String name = tv.getText().toString();
Intent data = new Intent();
data.putExtra("name", name);
setResult(3, data);
// 可以把当前的activity给结束掉
finish();
}
});
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(ContactActivity.this);
tv.setTextSize(28);
tv.setTextColor(Color.RED);
tv.setText(names.get(position));
return tv;
}
}
private List<String> getContacts (){
String rawbase = "content://com.android.contacts/raw_contacts";
String database = "content://com.android.contacts/data";
Cursor cursor = this.getContentResolver().query(Uri.parse(rawbase), null, null, null, null);
List<String> names = new ArrayList<String>();
while (cursor.moveToNext()) {
String id = cursor.getString( cursor.getColumnIndex("_id"));
System.out.println("id ="+ id);
Cursor datacursor = this.getContentResolver().query(Uri.parse(database), null, "raw_contact_id=?", new String[]{id}, null);
while (datacursor.moveToNext()) {
String type = datacursor.getString(datacursor.getColumnIndex("mimetype"));
if("vnd.android.cursor.item/name".equals(type)){
String data1 = datacursor.getString( datacursor.getColumnIndex("data1"));
names.add(data1);
}
}
datacursor.close();
}
cursor.close();
return names;
}
}
2.跳转Activity
package cn.itcast.mutileactivity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class Demo1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void open(View view){
// intent 意图 , 描述一个动作, 激活一个组件
/* 第一种写法
* Intent intent = new Intent();
intent.setClassName("cn.itcast.mutileactivity", "cn.itcast.mutileactivity.Demo2Activity");*/
// 第二种写法
// Intent intent = new Intent(this,Demo2Activity.class);
/*
*第三种写法
Intent intent = new Intent();
ComponentName component = new ComponentName("cn.itcast.mutileactivity", "cn.itcast.mutileactivity.Demo2Activity");
intent.setComponent(component);
*/
//前面的三种写法 要求必须要知道 被激活的组件的包名和 class的名字
//显示意图
//****************************************************
// Intent intent = new Intent();
// intent.setAction("cn.itcast.mutileactivity.xxx");
// intent.addCategory("android.xxx");
// intent.setData(Uri.parse("itcast://cn.itcast.demo"));
//intent.setType(type); // mime 数据类型 image/jpeg
// 隐式意图
// 只需要指定 intent的动作 ,intent的数据
// 如果我们想激活另外一个应用程序的组件
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_PICK);
//
// intent.setType("image/jpeg");
// intent 要去做一件事 泡 动作 泡茶 泡咖啡 泡妞 打人 打酱油
// intent.setData();指定数据的类型 type
// intent.addCategory();
/**
* <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android-dir/mms-sms" />
</intent-filter>
*/
/* Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("vnd.android-dir/mms-sms");*/
/**
* <intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
*/
Intent intent = new Intent();
intent.setAction("android.intent.action.SENDTO");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("smsto:123456"));
startActivity(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.mutileactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Demo1Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/activity02"
android:name=".Demo2Activity" ><intent-filter >
<action android:name="cn.itcast.mutileactivity.Demo2Activity" />
<action android:name="cn.itcast.mutileactivity.xxx" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.xxx" />
</intent-filter>
</activity>
</application>
</manifest>
3.onSaveInstance的使用
package cn.itcast.life;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
* 什么是生命周期 一个对象 从被创建出来new() 到 被销毁 (垃圾回收)这个过程当前必须执行的方法
*
* @author zehua
*
*/
public class DemoActivity extends Activity {
public void click(View view){
Intent intent = new Intent(this,Dmeo2Activity.class);
startActivity(intent);
}
/**
* activity第一次被创建的时候 执行的方法
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int page = 0;
//pdf 阅读器 当前用户读到了第30页
if(savedInstanceState!=null){
page = savedInstanceState.getInt("page");
System.out.println("定位到 第 "+ page+"页");
}
System.out.println("oncreate");
}
/**
* 在activity被异常 回收的时候 会被执行
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("page", 30);
}
/**
* 用户可见的时候 调用onstart();
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
// System.out.println("判断是否有暂停的位置 ,从暂停的位置继续播放");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
// System.out.println("暂停视频播放");
super.onStop();
System.out.println("onStop");
}
/**
* activity 被销毁的时候 执行的方法
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
}
}