Intent中指定程序要执行的动作(如:view,edit,dial)以及执行到该动作时所需要的资料,最后调用startActivity()。
1、作用
程序跳转、传递数据
Action/Data/Type/Catogory(对执行动作的附加信息进行描述)/Extras(其它附加信息)/Component(对目标组件的描述)
2、3种形式startActivity()/startActivityForResult()
BroadcastReceiver startService(Intent)和bindService(Intent,ServiceConnection,int)3、相关方法
intent.putExtra(name,value);
intent.putExtras(bundle); intent.getExtra(); getIntent();4、常用action
Intent.ACTION_DIAL
Intent.ACTION_CALL Intent.ACTION_VIEW Intent.ACTION_EDIT Intent.ACTION_DELETE Intent.ACTION_PICK(选择数据)//这是一个MainActivity,会分别发送startActivity和startActivityForResultpackage com.browan.server;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { protected static final int REQUST_CODE = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//相关布局模板文件main.xml find_and_modif_button(); //startActivityForResult示例 Button.OnClickListener listener1 = new Button.OnClickListener() { public void onClick(View v) { Intent intent1 = new Intent(MainActivity.this,Activity1.class); intent1.putExtra("MainActivity", "这是来自MainActivity的数据。"); startActivityForResult(intent1,REQUST_CODE); } }; //startActivity示例 Button.OnClickListener listener2 = new Button.OnClickListener() { public void onClick(View v) { setTitle("这是在MainActivity"); Intent intent2 = new Intent(MainActivity.this,Activity2.class); startActivity(intent2); } }; Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(listener1); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(listener2); } //对于返回结果的反映,startActivityForResult返回的结果指令的处理 protected void onActivityResult(int requst_code,int result_code,Intent data){ if(requst_code == REQUST_CODE) { if(result_code == RESULT_CANCELED) { setTitle("取消"); } else if(result_code == RESULT_OK) { String temp = ""; Bundle extras = data.getExtras(); if(extras != null) { temp = extras.getString("store"); } setTitle(temp); } else { setTitle("Others"); } } } }
下面再来看看Activity1.class部分:
package com.browan.server;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Activity1 extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity1); Button.OnClickListener listener3 = new Button.OnClickListener(){ public void onClick(View v){ Bundle bundle = new Bundle(); bundle.putString("store", "这是Activity1返回的数据。"); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK,mIntent); //这个就是返回去的消息,setResult在收到消息之后才会被触发返回发送 //MainActivity里面的onActivityResult就会对intent里面的result_code(RESULT_OK)处理 finish(); } }; Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(listener3); Bundle extras = getIntent().getExtras();//这里读取到了MainActivity传来的消息内容 String data = null; if(extras != null) { data = extras.getString("MainActivity"); } setTitle("现在是在Activity1里:" + data); }}
Activity2.class:
package com.browan.server;import android.app.Activity;import android.os.Bundle;public class Activity2 extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); setTitle("这里是在Activity2里。"); }}
xml各个的布局:
5.5 activity1.xml 5.6 activity2.xml
AndroidManifest. xml