Android 使用Intent隐式传递启动Activity

Intent传送分为显示和隐式传递

显示Intent传递


   
   
  1. Intent intent = new Intent(MainActivity. this,SecondActivity.class);
  2. startActivity(intent);

这个非常简单,只需要传入两个相关联上下文。

第一个:MainActivity.this就是指定当前的Activity

第二个:SecondActivity.class就是你要跳转的Activity

是不是非常简单???(前提得要在AndroidManifest进行Activity注册).


隐式Intent传递

隐式传递相对于显示Intent较委婉,不过也非常简单。

在AdnroidManifest.xml文件里


   
   
  1. <activity android:name= ".MainActivity">
  2. <intent-filter>
  3. <action android:name= "android.intent.action.MAIN"/>
  4. <category android:name= "android.intent.category.LAUNCHER" />
  5. </intent-filter>
  6. </activity>
  7. <activity android:name= ".SecondActivity">
  8. <intent-filter>
  9. <action android:name= "com.example.SECOND_ACTIVITY"/>
  10. <category android:name= "android.intent.category.DEFAULT"></category>
  11. </intent-filter>
  12. </activity>

在<intent-filter>标签里有非常重要的两个<action>和<category>两标签,这两个是必须同时存在的。

<action>指明了我们当前需要响应的activiity

<category>是附加信息


   
   
  1. button.setOnClickListener( new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. Intent intent = new Intent( "com.example.SECOND_ACTIVITY");
  5. startActivity(intent);
  6. }
  7. });

所以,当我们使用隐式传递时,只需要传递SecondActiviity里的<action>里的com.example.SECOND_ACTIVITY(这个可以自己随便取)。

 <category android:name="android.intent.category.DEFAULT"></category>
   
   

表示当前的category是默认的,在使用IIntent时就会自动调用。

重点:每个Intent只能指定一个action,可以有多个actegory.


   
   
  1. Intent intent = new Intent( "com.example.SECOND_ACTIVITY");
  2. intent.addCategory( "second");
  3. startActivity(intent);

在这里添加了intent.addCategory("second");必须在AndroidManifest.xml里添加对应的category,如下:


   
   
  1.         <activity android:name= ".SecondActivity">
  2. <intent-filter>
  3. <action android:name= "com.example.SECOND_ACTIVITY"/>
  4. <category android:name= "android.intent.category.DEFAULT"></category>
  5. <category android:name= "second"/>
  6. </intent-filter>
  7. </activity>

还有很多隐式Intent用法:比如


   
   
  1. Intent intent = new Intent(Intent.ACTION_VIEW);
  2. intent.setData(Uri.parse( "http://www.baidu.com"));
  3. startActivity(intent);

Intent.ACTION_VIEW是系统内置的。就可以打开自己手机浏览器,进行访问。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个使用隐式intent启动Activity完成数据传递和结果返回的样例代码: 在调用方Activity中: ``` // 创建一个用于传递数据的Bundle对象 Bundle data = new Bundle(); data.putString("name", "John"); data.putInt("age", 25); // 创建一个intent对象,设置action和category Intent intent = new Intent(); intent.setAction("com.example.ACTION_VIEW"); intent.addCategory("android.intent.category.DEFAULT"); // 将数据附加到intentintent.putExtras(data); // 启动目标Activity并等待返回结果 startActivityForResult(intent, 1); ``` 在目标Activity中: ``` // 从intent中获取传递的数据 String name = getIntent().getStringExtra("name"); int age = getIntent().getIntExtra("age", 0); // 创建一个用于返回结果的intent对象 Intent resultIntent = new Intent(); resultIntent.putExtra("result", "success"); // 将结果设置到intent中并返回 setResult(Activity.RESULT_OK, resultIntent); finish(); ``` 在调用方Activity中获取返回结果: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == Activity.RESULT_OK) { String result = data.getStringExtra("result"); Toast.makeText(this, "Result: " + result, Toast.LENGTH_SHORT).show(); } } ``` 以上代码用于启动一个名为"com.example.ACTION_VIEW"的Activity,将数据"name"和"age"传递到目标Activity中,并等待目标Activity返回结果。在目标Activity中,获取传递的数据并设置返回结果。最后,在调用方Activity中获取返回结果并显示一个Toast提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值