Intent的官方定义:An intent is an abstract description of an operation to be performed。一个Intent就是一次对将要执行的操作的抽象描述。
l 启动一个特定的Activity,代码如下:
1: Intent intent=new Intent(CurrentActivity.this,OtherActivity.class);2: startActivity (intent);
其中CurrentActivity是当前的Activity,OtherActivity是要启动的那个Activity,这就类似于窗体之间的跳转,只不过和.NET不同的是指明了谁在新“窗体”之前(或者说谁产生了新“窗体”)。
l 启动一个未指明的Activity,代码如下:
1: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:115-1345"));2: startActivity(intent);
以上代码没有指明要启动那个Activity只是把要启动的那个Activity的描述放在了Intent中,让系统去查找符合这个描述的Activity,然后启动并执行。
l 处理一个Activity的返回值,界面设计以及代码如下:
ActivityMain:
1: package com.eoeAndroid.activity;
2: import android.app.Activity;
3: import android.content.Intent;
4: import android.net.Uri;
5: import android.os.Bundle;
6: import android.view.View;
7: import android.view.View.OnClickListener;
8: import android.widget.Button;
9: public class ActivityMain extends Activity
10: {
11: OnClickListener listener1=null;
12: OnClickListener listener2=null;
13: Button button1;
14: Button button2;
15: static final int REQUEST_CODE=1;
16: @Override
17: public void onCreate(Bundle savedInstanceState)
18: {
19: super.onCreate(savedInstanceState);
20: listener1=new OnClickListener()
21: {
22: @Override
23: public void onClick(View v)
24: {
25: Intent intent1=new Intent(ActivityMain.this,Activity1.class);
26: intent1.putExtra("activityMain", "数据来自activityMain");
27: startActivityForResult(intent1, REQUEST_CODE);
28: }
29: };
30: listener2 =new OnClickListener()
31: {
32: @Override
33: public void onClick(View v)
34: {
35: setTitle("这是在ActivityMain");
36: Intent intent2=new Intent(ActivityMain.this,Activity2.class);
37: startActivity(intent2);
38: }
39: };
40: setContentView(R.layout.main);
41: button1=(Button)findViewById(R.id.button1);
42: button1.setOnClickListener(listener1);
43: button2=(Button)findViewById(R.id.button2);
44: button2.setOnClickListener(listener2);
45: setTitle("ActivityMain");
46: }
47: @Override
48: protected void onActivityResult(int requestCode,int resuleCode,Intent data)
49: {
50: if (requestCode==REQUEST_CODE)
51: {
52: if (resuleCode==RESULT_CANCELED)
53: setTitle("取消");
54: else if (resuleCode==RESULT_OK)
55: {
56: String temp=null;
57: Bundle extras=data.getExtras();
58: if (extras!=null)
59: {
60: temp=extras.getString("stroe");
61: }
62: setTitle(temp);
63: }
64: }
65: }
66: }
Activity1:
1: package com.eoeAndroid.activity;
2: import android.app.Activity;
3: import android.content.Intent;
4: import android.os.Bundle;
5: import android.view.View;
6: import android.view.View.OnClickListener;
7: import android.widget.Button;
8: public class Activity1 extends Activity {
9: OnClickListener listener1 = null;
10: Button button1;
11: /** Called when the activity is first created. */
12: @Override
13: public void onCreate(Bundle savedInstanceState) {
14: super.onCreate(savedInstanceState);
15: setContentView(R.layout.activity1);
16: listener1 = new OnClickListener() {
17: public void onClick(View v) {
18: Bundle bundle = new Bundle();
19: bundle.putString("store", "数据来自Activity1");
20: Intent mIntent = new Intent();
21: mIntent.putExtras(bundle);
22: setResult(RESULT_OK, mIntent);
23: finish();
24: }
25: };
26: button1 = (Button) findViewById(R.id.button3);
27: button1.setOnClickListener(listener1);
28: String data=null;
29: Bundle extras = getIntent().getExtras();
30: if (extras != null) {
31: data = extras.getString("activityMain");
32: }
33: setTitle("现在是在Activity1里:"+data);
34: }
35: }
Activity2:
1: package com.eoeAndroid.activity;
2: import android.app.Activity;
3: import android.os.Bundle;
4: import android.view.View;
5: import android.view.View.OnClickListener;
6: import android.widget.Button;
7: public class Activity2 extends Activity {
8: OnClickListener listener = null;
9: Button button;
10: /** Called when the activity is first created. */
11: @Override
12: public void onCreate(Bundle savedInstanceState) {
13: super.onCreate(savedInstanceState);
14: setContentView(R.layout.activity2);
15: listener = new OnClickListener() {
16: public void onClick(View v) {
17: finish();
18: }
19: };
20: button = (Button) findViewById(R.id.button4);
21: button.setOnClickListener(listener);
22: setTitle("现在是在Activity2里");
23: }
24: }