为了账号安全,请及时绑定邮箱和手机立即绑定

【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent

标签:
Android
1.0 在helloworld项目基础上创建活动SecondActivity:

webp

image

2.0 其中main.xml:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><item
    android:id="@+id/add_item"
    android:title="添加"/><item
    android:id="@+id/remove_item"
    android:title="移除"
    /></menu>
3.0 activity_second.xml不做修改。
4.0 SecondActivity.java:
package com.example.helloworld;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;public class SecondActivity extends AppCompatActivity {    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        //通过getMenuInflater()方法能够调用MenuInflate对象,
        // 在调用它的inflate()方法就可以给当前活动创建菜单。
        //inflate()方法第一个参数我们通过哪一个资源文件来创建菜单(R.menu.main)
        //第二个参数指定我们的菜单项将添加到哪一个Menu对象中
        //使用onCreateOptionsMenu()方法传入menu参数,再用这个方法返回true,表示允许创建的菜单显示出来
        //如果返回false,创建的菜单将无法显示。
        getMenuInflater().inflate(R.menu.main, menu);        return true;//        return super.onCreateOptionsMenu(menu);
    }    @Override
    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case R.id.add_item:
                Toast.makeText(this, "你点击了添加", Toast.LENGTH_SHORT).show();                break;            case R.id.remove_item:
                Toast.makeText(this, "你点击了删除", Toast.LENGTH_SHORT).show();                break;            default:

        }        return true;//        return super.onOptionsItemSelected(item);
    }    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

    }
}

其中的onCreateOptionsMenu()方法和onOptionsItemSelected()方法可以通过Ctrl+O键(Mac系统是control+O)调用。

5.0  AndroidMainifest.xml:
<activity android:name=".SecondActivity"
            android:label="第二个活动">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

运行:

webp

image

webp

image

webp

image

6.0 Intent一般用于启动活动、启动服务以及发送广播等场景,其中显式Intent,Intent传入两个参数:第一个是上下文,第二个是目标活动:

新建活动ThirdActivity, 勾选“Generate Layout File”和“Launcher Activity”。

ThirdActivity.java:

package com.example.helloworld;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class ThirdActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        Button button3 = (Button) findViewById(R.id.button_3);
        button3.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {//                Toast.makeText(FirstActivity.this,"你点击了按钮1",Toast.LENGTH_SHORT).show();
                //Intent一般用于启动活动、启动服务以及发送广播等场景
                //显式Intent,Intent传入两个参数:第一个是上下文,第二个是目标活动
                Intent intent = new Intent(ThirdActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_third.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ThirdActivity">

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮 3"
        /></android.support.constraint.ConstraintLayout>

运行(点击“按钮3”,进入第二个活动):

webp

image

webp

image

7.0 而隐式Intent,<action>和<category>需要同时匹配上才能响应,一个是"com.example.helloworld.ACTION_START",一个是一个默认的category("android.intent.category.DEFAULT"),调用startActivity时会自动将其添加到Intent。

同样,新建活动FourthActivity,勾选“Generate Layout File”。

修改AndroidMainifest.xml关于活动“FourthActivity”的内容:

 <activity android:name=".FourthActivity"
            android:label="第四个活动">
            <intent-filter>
                <action android:name="com.example.helloworld.ACTION_START" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.helloworld.MY_CATEGORY" />
            </intent-filter>
        </activity>

第一个action,当前活动可以响应"com.example.helloworld.ACTION_START"这个action,只有当action和category都匹配上才能激活该活动,第一个category是系统默认的配置,在Intent匹配的时候可以不提供category。

举例,我在SecondActivity活动中,设置了Intent,提供了action和category匹配,如果不提供category,发现就会像6.0里面一样,活动FourthActivity可以匹配得到,就会激活(打开)活动FourthActivity,用了第二个category 就会匹配到活动FifthActivity。

进一步修改AndroidMainifest.xml关于活动“FourthActivity”的内容:

 <activity
            android:name=".FourthActivity"
            android:label="第四个活动">
            <intent-filter>
                <action android:name="com.example.helloworld.ACTION_START" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

新建活动FifthActivity,勾选“Generate Layout File”。

修改AndroidMainifest.xml关于活动“FifthActivity”的内容:

<activity android:name=".FifthActivity"
            android:label="第五个活动">
            <intent-filter>
                <action android:name="com.example.helloworld.ACTION_START" />
                <category android:name="com.example.helloworld.MY_CATEGORY" />
            </intent-filter>
        </activity>

这里需要修改下活动SecondActivity.java的代码:

package com.example.helloworld;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.Toast;public class SecondActivity extends AppCompatActivity {    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        //通过getMenuInflater()方法能够调用MenuInflate对象,
        // 在调用它的inflate()方法就可以给当前活动创建菜单。
        //inflate()方法第一个参数我们通过哪一个资源文件来创建菜单(R.menu.main)
        //第二个参数指定我们的菜单项将添加到哪一个Menu对象中
        //使用onCreateOptionsMenu()方法传入menu参数,再用这个方法返回true,表示允许创建的菜单显示出来
        //如果返回false,创建的菜单将无法显示。
        getMenuInflater().inflate(R.menu.main, menu);        return true;//        return super.onCreateOptionsMenu(menu);
    }    @Override
    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case R.id.add_item:
                Toast.makeText(this, "你点击了添加", Toast.LENGTH_SHORT).show();                break;            case R.id.remove_item:
                Toast.makeText(this, "你点击了删除", Toast.LENGTH_SHORT).show();                break;            default:

        }        return true;//        return super.onOptionsItemSelected(item);
    }    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {              //Intent一般用于启动活动、启动服务以及发送广播等场景
                //隐式Intent,<action>和<category>需要同时匹配上才能响应,
                //一个是"com.example.helloworld.ACTION_START",
                //一个是一个默认的category("android.intent.category.DEFAULT"),
                //调用startActivity时会自动将其添加到Intent。
                Intent intent = new Intent("com.example.helloworld.ACTION_START");
                startActivity(intent);
            }
        });
    }
}

修改活动FourthActivity.java的代码:

package com.example.helloworld;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class FourthActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fourth);
        Button button4 = (Button) findViewById(R.id.button_4);
        button4.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {                //Intent一般用于启动活动、启动服务以及发送广播等场景
                //隐式Intent,<action>和<category>需要同时匹配上才能响应,
                //一个是"com.example.helloworld.ACTION_START",
                //一个是一个默认的category("android.intent.category.DEFAULT"),
                //调用startActivity时会自动将其添加到Intent。

                Intent intent = new Intent("com.example.helloworld.ACTION_AA");
                intent.addCategory("com.example.helloworld.MY_CATEGORY");
                startActivity(intent);
            }
        });
    }
}

activity_second.xml:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮 2 指向4"
        /></android.support.constraint.ConstraintLayout>

运行测试:

webp

image

webp

image

webp

image

点击“按钮4 指向5”,程序奔溃,目前找不到原因……

8.0 更多Intent运用,可以实现多个应用程序之间实现功能共享。

新建活动SixthActivity.java,勾选“Generate Layout File”和“Launcher Activity”。

修改AndroidMainifest.xml关于活动“SixthActivity”的内容:

<activity android:name=".SixthActivity"
            android:label="第六个活动">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

activity_sixth.xml:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SixthActivity">

    <Button
        android:id="@+id/button_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮 6 指向浏览器支撑网站"
        /></android.support.constraint.ConstraintLayout>

活动SixthActivity.java:

package com.example.helloworld;import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SixthActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sixth);
        Button button6 = (Button) findViewById(R.id.button_6);
        button6.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {//                指定Intent的action是Intent.ACTION_VIEW//                这是一个Android系统内置的动作,常量值为android.intent.action.ACTION_VIEW//                通过Uri.parse()方法,将一个网址字符串解析成uri对象,//                再调用Intent的setData()方法将URI对象传递进去。
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.cnblogs.com/xiaofu007/"));
                startActivity(intent);
            }
        });
    }
}

运行:

webp

image

webp

image

9.0 setData()方法,这个方法的使用可以结合在<intent-filter>标签中再配置一个<data>标签。

android:scheme:用于指定数据的协议部分,例如http部分

android:host:用于指定数据的主机名部分,例如www.baidu.com

android:port:用于指定数据的端口部分,一般紧随在主机名之后

android:path:用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容。

android:mimeType:用于指定可以处理的数据额、类型,允许使用通配符的方式进行指定。

只有<data>标签中指定内容和Intent中携带的Data完全一致时,当前活动才会完全响应Intent。

不过一般<data>标签不好记指定过多内容,比如指定android:scheme为http就可以响应所有Intent了。

新建活动SeventhActivity,勾选“Generate Layout File”和“Launcher Activity”。

修改AndroidMainifest.xml关于活动“SeventhActivity”的内容:

 <activity
            android:name=".SeventhActivity"
            android:label="第七个活动">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <data android:scheme="http"/>
            </intent-filter>
        </activity>

SeventhActivity.java:

package com.example.helloworld;import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SeventhActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seventh);
        Button button7 = (Button) findViewById(R.id.button_7);
        button7.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.baidu.com"));
                startActivity(intent);
            }
        });
    }
}

activity_seventh.xml:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SeventhActivity">

    <Button
        android:id="@+id/button_7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮 7 指向浏览器支撑网站"
        /></android.support.constraint.ConstraintLayout>

运行:

webp

image

webp

image

10.0  除了http之外,还可以指定geo,表示显示地理位置,tel表示拨打电话号码:
button7.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {//                Intent intent = new Intent(Intent.ACTION_VIEW);//                intent.setData(Uri.parse("https://www.baidu.com"));//                startActivity(intent);
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10010"));
                startActivity(intent);
            }
        });

相应的修改AndroidMainifest.xml关于活动“SeventhActivity”<data>标签内容为tel

运行:

webp

image

webp

image



作者:我睡醒刚刚
链接:https://www.jianshu.com/p/8f08b7bbac14


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消