zoukankan      html  css  js  c++  java
  • 在 Android 中 Intent 的概念及应用

    一、显式Intent:
    startActivity(new Intent(MainActivity.this, 类名.class));
     

    二、隐式Intent:

    1.在AndroidManiFest.xml 文件的<application>标签中注册 <activity>标签,形如 .类名:
      <activity android:name=".Another" android:exported="false" >
                <intent-filter >
                    <action android:name="com.example.jikexueyuan_learnintent.intent.action.another"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
       </activity>
            注:exported属性为是否可以在其他其他APP中引用或者打开此activity,即是否可导出
                   intet-filter中,<action>标签,给该activity 取一个名字,任意(一般为:包名.intent.action.类名)
                                           <category>标签,一般是android.intent.category.DEFAULT 。
    2.在调用的类中, startActivity(new Intent(" intet-filter中,<action>标签,给该activity 取的名字"));
    findViewById(R.id.btn_startAnotherAty).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try {
                        startActivity(new Intent("com.example.jikexueyuan_learnintent.intent.action.another"));
                    } catch (Exception e) {
                        // TODO: handle exception
                        Toast.makeText(MainActivity.this, "无法启动", Toast.LENGTH_SHORT).show(); //吐司的用法
                    }
                }
        });
    3.也可直接在要另一个要打开的类B中,定义一个     公有静态常量字符串
    public static final String Action = "com.example.jikexueyuan_learnintent.intent.action.another";
    然后在调用时,将activity 名改为类名 B.ACTION,这样比较容易理解。
                  即:        startActivity(new Intent(Another.Action));
     
    三、Intent 过滤器相关选项:
              在一个APP中新建两个APP(APP1和APP2),在AndroidManiFest.xml 文件的<application>标签中注册 <activity>标签,形如 .类名;在intet-filter中,<action>标签,给这;两个activity 取同一个名字。
    通过APP3调用程序  startActivity(new Intent("名字"));结果如下图所示
    会提示您是选择那个APP运行(1仅运行一次2设为默认),若设APP1为默认,要取消,只需在
    设置-》应用程序-》APP1-》(应用程序信息中的默认启动)清楚默认设置
            在其中一个APP中(如APP1),在AndroidManiFest.xml 文件的<application>的<intent-filter >中增加data标签,并定义一个协议为APP:        <data android:scheme="app"/>   (去查看其它标签的意义)
    回到APP3的调用程序  startActivity(new Intent("名字"));加, Uri.parse("app://")字段。
           即      startActivity(new Intent("名字", Uri.parse("app://")));     (app://后面可添加任意字段湖或者不添加)
    以一颗童心善待生活
  • 相关阅读:
    jsp JavaBean配置
    jsp response对象
    tomcat 多servlet配置
    jsp session
    什么才是永恒?
    Class Diagram Of elvish ray 0.6 For Reference
    Dataflow
    过生日
    我们开始设计软件架构
    寻回旧作——Mars Princess
  • 原文地址:https://www.cnblogs.com/lizhilin2016/p/5251830.html
Copyright © 2011-2022 走看看