android基础--意图的学习

来源:互联网 发布:淘宝首页流量入口 编辑:程序博客网 时间:2024/05/24 01:42
 

Main.xml

<LinearLayout>

    <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:id="@+id/button"

    android:text="@string/button"/>

</LinearLayout>

Other.xml

<LinearLayout>

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        Button button = (Button) this.findViewById(R.id.button);

        button.setOnClickListener(new ButtonClickListener());

    }

    private final class ButtonClickListener implements View.OnClickListener{

              public void onClick(View v) {

                     Intent intent = new Intent();

                     //采用隐式意图激活OtherActivity,下面两个参数在Manifest中配置好了

                     intent.setAction("cn.com.me");//传入动作参数

                     //intent.addCategory("android.intent.category.DEFAULT");//传入类别参数

                     startActivity(intent);//方法内回味intent设置类别android.intent.category.DEFAULT上面的传入类别参数的代码不用要

              }

    }

}

otherActivity.java

public class OtherActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.other);

    }

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="cn.com.intent"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <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>

       <!-- 完成对OtherActivity的配置 -->

       <activity android:name=".OtherActivity" android:label="@string/otheractivity">

           <!-- 配置意图过滤器android:name="cn.com.me"动作名称 -->

           <intent-filter>

              <!-- 动作参数 -->

              <action android:name="cn.com.me"/>

              <!-- 类别参数 -->

              <category android:name="android.intent.category.DEFAULT"/>

           </intent-filter>

       </activity>

    </application>

    <uses-sdk android:minSdkVersion="8" />

</manifest>

有数据参数时候的匹配情况举例:

<!-- 数据参数 -->

<data android:scheme="aaa" android:host="bbb" android:path="/ccc" android:mimeType="text/plain"/>

对应的设置数据参数方法

intent.setDataAndType(Uri.parse("aaa://bbb/ccc"), "text/plain");

不要采用这种方法:

intent.setData(Uri.parse("aaa://bbb/ccc"));

intent.setType("text/plain");

因为setType()会把setData()清空,从而产生意图不匹配的错误

采用隐式意图时:如果没有数据参数的情况下,只要意图对象中设置的动作和类别都出现在意图过滤器中,就能够跟过滤器匹配

注意:采用显示意图的话

<intent-filter>

       <!-- 动作参数 -->

       action android:name="cn.com.me"/>

       <!-- 类别参数 -->

       <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

意图过滤器不用配置

用这个方法即可

intent.setClass(getApplicationContext(), OtherActivity.class);

在同一个应用中采用显示意图比较好,因为需要查找,所以用显示意图性能高一些

应用与应用之间的相互激活采用隐式意图比较好