Intent之学习一(显式与隐式)

本文介绍了Intent在Android应用中通过Activity间的启动跳转的使用方法,包括显式Intent和隐式Intent的区别及具体实现案例。

Intent之学习一(显式与隐式)

通过Activity间的启动跳转,学习Intent的使用。

  • 1、显式Intent
  • 2、隐式Intent

1、显式Intent

显式 Intent常使用如下构造函数:
Intent(Context packageContext, Class <?> cls)
其中,第一个参数指当前上下文,即当前Activity。第二个参数指要启动的目标Activity。
接下来,实现在FirstActivity点击Button,启动SecondActivity。

代码块

FirstActivity.class:

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_first.xml:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text1"/>
<Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button1"/>

SecondActivity.class:

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

activity_second.xml:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text2"/>

AndroidManifest.xml:

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

2、隐式Intent

隐式Intent常用如下构造函数:

Intent(String action)

2.1 启动程序内Activity(默认category)

代码块

FirstActivity.class:

//隐式intent启动activity2
        Button button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.test.ACTION_START");
                startActivity(intent);
            }
        });

activity_first.xml:

<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button2"/>

AndroidManifest.xml:

<activity android:name=".SecondActivity">
    <intent-filter>
        <action android:name="com.example.test.ACTION_START"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

2.2 启动程序内Activity(自定义category)

代码块

FirstActivity.class:

//隐式intent启动activity3
        Button button3 = (Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.test.ACTION_START");
                intent.addCategory("com.example.test.MY_CATEGORY");
                startActivity(intent);
            }
        });

AndroidManifest.xml:

<activity android:name=".SecondActivity">
    <intent-filter>
        <action android:name="com.example.test.ACTION_START"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="com.example.test.MY_CATEGORY"/>
    </intent-filter>
</activity>

2.3 启动其他程序Activity

代码块

FirstActivity.class:

//隐式intent启动浏览器
        Button button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开百度
                 Intent intent = new Intent(Intent.ACTION_VIEW);          
intent.setData(Uri.parse("http://www.baidu.com"));
//        //拨打电话
//        Intent intent = new Intent(Intent.ACTION_DIAL);
//        intent.setData(Uri.parse("tel:10086"));
//        //打开地图
//        Intent intent = new Intent(Intent.ACTION_VIEW);              //intent.setData(Uri.parse("geo:38.899533,-77.036476"));
                startActivity(intent);
            }
        });

目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值