您的位置:首页 > 移动开发 > Android开发

Android应用程序四大组件之Activity(二)

2011-09-05 23:15 381 查看

Activity状态保存

1.onSaveInstanceState()/onCreate()/onRestoreInstanceState()

系统在回收当期Activity之前会调用onSaveInstanceState(),Back&Home两个键不会调用此方法.例如电子书程序,阅读到某一页内存不足被系统回收.可以通过此方法记录第几页.在程序重新启动时,在onCreate()里判断重新读取退出时第几页状态.它的参数Bundle是key-value形式

privatefinalStringSAVE_INSTANCE_TAG="MainTestActivity.Saveinstancetag";
EditTexteditText=null;

@Override
protectedvoidonSaveInstanceState(BundleoutState){

if(outState!=null){
outState.putString(this.SAVE_INSTANCE_TAG,"hello!!!!");
}
System.out.println("onSaveInstanceState");

super.onSaveInstanceState(outState);
}
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

System.out.println("onCreate");
editText=(EditText)findViewById(R.id.myEditText);

if(editText!=null&&savedInstanceState!=null){
editText.setText(savedInstanceState.getString(SAVE_INSTANCE_TAG));
}
if(savedInstanceState!=null){
System.out.println("savedInstanceState"+savedInstanceState);
}
}

onRestoreInstanceState()不一定是与onSaveInstanceState()成对使用的.调用此方法时.Activity定是被系统Destroy掉的.其Bundle参数值也可以传到onCreate()

@Override
protectedvoidonRestoreInstanceState(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
Stringtag=savedInstanceState.getString(this.SAVE_INSTANCE_TAG);
super.onRestoreInstanceState(savedInstanceState);
}



2.借助ActivityLifecycle+Preference

使用这种方式状态保存可以是.横竖屏幕切换,按HomeorBack键之后再回来.

@Override
protectedvoidonPause(){
//TODOAuto-generatedmethodstub
super.onPause();
//写入SharedPreferences
SharedPreferencespreferences=getSharedPreferences("name",
MODE_PRIVATE);
Editoreditor=preferences.edit();
editor.putBoolean("boolean_key",true);
editor.putString("string_key","string_value");
editor.commit();

}
@Override
protectedvoidonResume(){
//TODOAuto-generatedmethodstub
super.onResume();
//读取SharedPreferences
SharedPreferencespreferences=getSharedPreferences("name",
MODE_PRIVATE);
preferences.getBoolean("boolean_key",false);
preferences.getString("string_key","default_value");
}



Activity之间通信

1.无参数Activity跳转

Intentintent=newIntent(Activity1.this,Activity2.class);
startActivity(intent);
[/code]

2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)

Intentintent=newIntent();
intent.setClass(Ex03_10Activity.this,Ex03_10_1.class);

Bundlebundle=newBundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
intent.putExtras(bundle);

startActivity(intent);//startActivityForResult(intent,0);
//数据的接收
Bundlebundle=this.getIntent().getExtras();

Stringsex=bundle.getString("sex");
doubleheight=bundle.getDouble("height");

3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)

Intentintent=getIntent();
Bundlebundle2=newBundle();
bundle2.putString("sex","Man!");
intent.putExtras(bundle2);
setResult(RESULT_OK,intent);

4.回调上一个Activity的结果处理函数(onActivityResult)

	@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
//TODOAuto-generatedmethodstub
switch(requestCode){
caseRESULT_OK:
Bundlebundle=data.getExtras();
Stringsex=bundle.getString("sex");
doubleheight=bundle.getDouble("height");
et.setText(""+height);
if(sex.equals("M")){
rb1.setChecked(true);
}else{
rb2.setChecked(true);
}
break;

default:
break;
}
}

打电话

1.//叫出拨号程序

2.Uriuri=Uri.parse("tel:0800000123");

3.Intentit=newIntent(Intent.ACTION_DIAL,uri);

4.startActivity(it);

1.//直接打电话出去

2.Uriuri=Uri.parse("tel:0800000123");

3.Intentit=newIntent(Intent.ACTION_CALL,uri);

4.startActivity(it);

5.//用這個,要在AndroidManifest.xml中,加上

6.//<uses-permissionid="android.permission.CALL_PHONE"/>

传送SMS/MMS

1.//调用短信程序

2.Intentit=newIntent(Intent.ACTION_VIEW,uri);

3.it.putExtra("sms_body","TheSMStext");

4.it.setType("vnd.android-dir/mms-sms");

5.startActivity(it);

1.//传送消息

2.Uriuri=Uri.parse("smsto://0800000123");

3.Intentit=newIntent(Intent.ACTION_SENDTO,uri);

4.it.putExtra("sms_body","TheSMStext");

5.startActivity(it);

1.//传送MMS

2.Uriuri=Uri.parse("content://media/external/images/media/23");

3.Intentit=newIntent(Intent.ACTION_SEND);

4.it.putExtra("sms_body","sometext");

5.it.putExtra(Intent.EXTRA_STREAM,uri);

6.it.setType("image/png");

7.startActivity(it);

以上应用实例更多请参考原作者/article/4716176.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: