前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Intent在活动之间穿梭

使用Intent在活动之间穿梭

作者头像
Dream城堡
发布2018-12-12 15:30:32
5770
发布2018-12-12 15:30:32
举报
文章被收录于专栏:Spring相关Spring相关

使用Intent在活动之间穿梭

1.在com.example.activitytest中创建第二个活动SecondActivity:
代码语言:javascript
复制
/**
 * 第二个活动
 */
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
    }
}

创建完成后会自动生成second_layout.xml,这里我们进行修改如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        />
</LinearLayout>

这时AndroidManifest.xml已经帮我们注册了活动:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".FirstActivity"
            android:label="this is first">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

</manifest>
2.使用Intent启动活动

Intent是Android中各组件进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据.

Intent大致可以分为两种:显示Intent和隐式Intent

一.显式Intent

Intent中有多个构造函数的重载,其中一个Intent(Context packageContext,Class<?> cls),这个构造函数第一个参数是启动活动的上下文,第二个启动活动的目标.

代码语言:javascript
复制
            //启动第二个活动
            public void onClick(View v) {
            //FirstActivity.this 指上下文 SecondActivity.class指活动目标
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivity(intent);
            }

二.隐式Intent

通过<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,

打开AndroidManifest.xml,添加如下代码:

代码语言:javascript
复制
 <activity android:name=".SecondActivity">
       <intent-filter>
           <action android:name="com.example.activitytest.ACTION_START" />
           <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
 </activity>

修改FirstActivity中按钮的点击事件:

代码语言:javascript
复制
 //隐式使用Intent
            public void onClick(View v) {
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                startActivity(intent);
            }

可以选择添加多个category:

代码语言:javascript
复制
//隐式使用Intent
            public void onClick(View v) {
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                intent.addCategory("com.example.activitytest.MY_CATEGORY");
                startActivity(intent);
            }

打开AndroidManifest.xml,添加如下代码:

代码语言:javascript
复制
<activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.activitytest.MY_CATEGORY" />
            </intent-filter>
        </activity>
3.Intent的其他使用方法

跳转第三方链接

代码语言:javascript
复制
//跳转第三方链接
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.baidu.com"));
                startActivity(intent);
            }

调用拨号功能

代码语言:javascript
复制
//调用拨号功能
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用Intent在活动之间穿梭
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档