zoukankan      html  css  js  c++  java
  • 显式intent和隐式intent

    android其中显式intent和隐式intent的差别
    定义:
      Intent定义:Intent是一种在不同组件之间传递的请求消息。是应用程序发出的请求和意图。

    作为一个完整的消息传递机制,Intent不仅须要发送端,还须要接收端。
      显式Intent定义:对于明白指出了目标组件名称的Intent。我们称之为显式Intent。
      隐式Intent定义:对于没有明白指出目标组件名称的Intent。则称之为隐式Intent。
            显示Intent直接指明了被启动的的类的定义 
            比方一个实例: 
    Mainactivity.java
    package com.example.root.longpra;

    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.TextView;


    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);/*绑定activity*/
            findViewById(R.id.startbtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this,AnoAct.class));
                    /*像这样的非常明白的指出了被启动的类的定义的就是显示Intent*/
                }
            });
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
    AnoAct.java
    package com.example.root.longpra;

    import android.app.Activity;
    import android.os.Bundle;

    /**
     * Created by root on 15-8-22.
     */
    public class AnoAct extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.anoact);/*绑定anoact*/
        }
    }

    通过字符串来启动activity
    AndroidManifest.xml
    <?

    xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.root.longpra" >

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".AnoAct"
                android:label="LongPra">
                <intent-filter>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <!--android.intent.category.DEFAULT指名这个intent-filter这个行为方式为一个activity-->
                    <action android:name="com.example.root.longpra.intent.action.AnoAct"/>
                    <!--action能够为随意字符串。仅仅要在启动的过程启用这个字符串就可以-->
                </intent-filter>>
            </activity>>
        </application>

    </manifest>

    MainActivity.java
    package com.example.root.longpra;

    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.TextView;


    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);/*绑定activity*/
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(AnoAct.ACTION));
                    /*像这样的非常明白的指出了被启动的类的定义的就是显示Intent*/
                }
            });
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
    AnoAct.java
    package com.example.root.longpra;

    import android.app.Activity;
    import android.app.Notification;
    import android.os.Bundle;

    /**
     * Created by root on 15-8-22.
     */
    public class AnoAct extends Activity {
        public static final String ACTION = "com.example.root.longpra.intent.action.AnoAct";//在这边定义这个字符串微一个常量。这样当外面须要调用这个activity时候能够直接使用ACTION
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.anoact);/*绑定anoact*/
        }
    }
    新建一个app1。在Mainctivity中
    package com.example.root.app1;

    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;


    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent("com.example.root.longpra.intent.action.AnoAct"));
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
    这样能够直接启动app中的AnoAct。以上就是隐示的intent
    说明:Android系统使用IntentFilter来寻找与隐式Intent相关的对象。
      详解:
      显式Intent直接用组件的名称定义目标组件,这样的方式非常直接。

    可是因为开发者往往并不清楚别的应用程序的组件名称。因此,显式Intent很多其它用于在应用程序内部传递消息。比方在某应用程序内,一个Activity启动一个Service。
      隐式Intent恰恰相反。它不会用组件名称定义须要激活的目标组件。它更广泛地用于在不同应用程序之间传递消息。
      在显式Intent消息中。决定目标组件的唯一要素就是组件名称,因此,假设你的Intent中已经明白定义了目标组件的名称。那么你就全然不用再定义其它Intent内容。
      而对于隐式Intent则不同。因为没有明白的目标组件名称。所以必须由Android系统帮助应用程序寻找与Intent请求意图最匹配的组件。
      Android系统寻找与Intent请求意图最匹配的组件详细的选择方法
    是:Android将Intent的请求内容和一个叫做IntentFilter的过滤器比較,IntentFilter中包括系统中全部可能的待选组件。
      假设IntentFilter中某一组件匹配隐式Intent请求的内容。那么Android就选择该组件作为该隐式Intent的目标组件。


      Android怎样知道应用程序可以处理某种类型的Intent请求呢?这须要应用程序在Android-Manifest.xml中声明自己所含组件的过滤器(就可以以匹配哪些Intent请求)。
      一个没有声明Intent-Filter的组件仅仅能响应指明自己名字的显式Intent请求,而无法响应隐式Intent请求。
      而一个声明了IntentFilter的组件既能够响应显式Intent请求。也能够响应隐式Intent请求。

    在通过和
    IntentFilter比較来解析隐式Intent请求时,Android将下面三个因素作为选择的參考标准。
      Action
      Data
      Category

      而Extra和Flag在解析收到Intent时是并不起作用的。


    钟志远  江苏南京  904727147

  • 相关阅读:
    Feign性能优化注意事项--超时
    Android MVP+Retrofit+dagger2+RxAndroid框架整合
    Android adb命令,linux中各种命令
    【视频音频】雷霄骅开源视音频项目汇总
    Android LitePal介绍与使用说明
    AndroidStudio第一次提交项目代码到git服务器/github
    Android框架之路——Fragmentation的使用(流式交互Demo)
    Android Studio 升级到3.0后出现编译错误.gradlecaches ransforms-1files-1.1*****-release.aar
    Android Studio你必须学会的快捷键(Eclipse转AS必看)
    Intellij IDEA常用配置详解
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7345397.html
Copyright © 2011-2022 走看看