Intent的使用方法和Activity之间的传值
Intent:意图,用于启动新的activity;
Intent可分为显式意图和隐式意图;
Intent.putExtra传递的对象必须是可序列化的(类必须实现序列化接口);
传送方:
intent = new Intent(this, IntentActivity.class); intent.putExtra("titie","你好吗?"); intent.putExtra("keys", "hahahaha"); startActivity(intent);
接收方:
Intent in = getIntent(); String tit = in.getStringExtra("titie"); String key = in.getStringExtra("keys"); tv = (TextView) findViewById(R.id.tvintent); tv.setText(tit+","+key);
使用静态成员类的方式:(传递的值为全局数据,项目内共享,所有的Activity均能获取到)
首先创建类,且类中的成员为静态的:
public class StaticClass { private static String name; private static String sex; private static double weight; public static String getName() { return name; } public static void setName(String name) { StaticClass.name = name; } public static String getSex() { return sex; } public static void setSex(String sex) { StaticClass.sex = sex; } public static double getWeight() { return weight; } public static void setWeight(double weight) { StaticClass.weight = weight; } }
发送方:
if(v!=null){ //启动新的activity并给其传递数据 Intent intent = new Intent(this,TargetActivity.class); //使用静态成员类的方式传递数据 StaticClass.setName("李四"); StaticClass.setSex("女"); StaticClass.setWeight(200); startActivity(intent); }
接收方:
//得到启动该 Activity的Intent对象 Intent intent = getIntent(); String name = StaticClass.getName(); String sex = StaticClass.getSex(); double weight = StaticClass.getWeight(); Toast.makeText(this,name+","+sex+","+weight, Toast.LENGTH_LONG).show();
利用Application传值:(传递的值为全局数据,项目内共享,所有的Activity均能获取到)
首先要创建Application的子类:
public class MyApplication extends Application { private String name; private String sex; private double weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
然后修改项目的配置文件:
也就是在application 中要指明application的子类;
发送方:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { if(v!=null) { //启动新的activity并给其传递数据 Intent intent = new Intent(this,TargetActivity.class); //getApplication()获取 Application对象 //使用全局的 Application对象传递数据, //先根据要传递的数据定义 Application类的子类 //然后去清单文件中注册该子类 MyApplication app = (MyApplication)getApplication(); app.setName("李四"); app.setSex("女"); app.setWeight(200); startActivity(intent); } } }
接收方:
public class TargetActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_target); //得到启动该 Activity的Intent对象 Intent intent = getIntent(); Application app = getApplication(); MyApplication mapp = (MyApplication)app; String name = mapp.getName(); String sex = mapp.getSex(); double weight = mapp.getWeight(); Toast.makeText(this, name+" "+sex+" "+weight, Toast.LENGTH_LONG).show(); } }
利用ForResult传值并接收方返回的数据
获取返回的数据:
1、使用startActivityForResult()方法来启动另一个Activity;
2、被启动的Activity使用Intent来传递返回的数据,使用setResult()方法来返回数据;
3、接收返回数据的Activity,通过执行回调方法onActivityResult()来处理返回的数据;
发送方:
public class MainActivity extends Activity { public static final int REQUESTCODE = 6; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btnClick(View v) { if(v!=null) { //启动另一个activity并获取返回的值 Intent intent = new Intent(this,TargetActivity.class); intent.putExtra("hello", "哥们,你好"); //该方法可以启动另一个 activity同时 可以获取返回的值 //第二个参数是请求码,一个int类型的数值 startActivityForResult(intent, REQUESTCODE); } } //当再次回到该activity时,获取返回的数据,执行了回调方法 //第三个参数接收返回的intent对象 //通过请求码和结果码来区分是哪一个activity返回的数据 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==REQUESTCODE && resultCode==this.RESULT_OK) { if(data!=null) { String value = data.getStringExtra("hai"); Toast.makeText(this, value, Toast.LENGTH_SHORT).show(); } } } }
接收方:
public class TargetActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_target); //获取传过来的值 Intent intent = getIntent(); String value = intent.getStringExtra("hello"); Toast.makeText(this, value, Toast.LENGTH_SHORT).show(); //设置返回的值 Intent result = new Intent(); result.putExtra("hai", "你好,哥们"); //把数据返回 //第一个参数是结果码 //当从该activity回到上一个activity时才把数据返回 setResult(this.RESULT_OK, result); } }
隐试意图:
一般用于启动系统应用,比如点击按钮拨打电话,点击按钮发送短信,点击按钮打开页面等