Android期末考考前盘点(六):1000%用到的Intent意图

Intent意图,是个非常基础的东西,因为贯穿应用始终,例如跳转Activity、打开Service、向Activity跳转并传递数据、回传数据等等......

首先要知道这个Intent,是一个系统类,所以你使用时,第一件事必然是new对象

Intent intent=new Intent(Lindd.this,Zhuzhendonghua.class);
//跳转Activity  Lindd到Zhuzhendonghua

上面的代码就是从Lindd这个Activity跳转到Zhuzhendonghua这个Actiyity

所以当想要跳转Activity的时候,第一个参数是上下文(粗暴的理解就是现在在哪),第二个参数就是去哪里

再执行以下代码即可跳转

startActivity(intent);//跳转

再比如,在Service服务中对服务进行启动:

Intent intent=new Intent(MainActivity.this, MyService.class);
startService(intent);

接下来就是其中较为复杂的跳转Activity并携带数据

这里,在课堂中教过使用Bundel

                Intent intent=new Intent(Lindd.this,ZIdingyikongjian.class);
                Bundle bundle=new Bundle();
                bundle.putString("account","lindd");
                bundle.putString("password","123123");
                intent.putExtras(bundle);
                startActivity(intent);

但是对于基础差的学生我建议老老实实的多put几个,如下:

                Intent intent=new Intent(Lindd.this,ZIdingyikongjian.class);
                intent.putExtra("account","lindd");
                intent.putExtra("password","123123");
                startActivity(intent);

对于法一,在跳转的Activity中如何获取?如下代码

               Bundle bundle= getIntent().getExtras();
               String account=bundle.getString("account");
               String password=bundle.getString("password");

对于法二,在跳转的Activity中如何获取?如下代码

扫描二维码关注公众号,回复: 15437647 查看本文章
                getIntent().getStringExtra("account");
                getIntent().getStringExtra("password");

猜你喜欢

转载自blog.csdn.net/m0_59558544/article/details/131332593