您的位置:首页 > 移动开发 > Android开发

Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值

2014-09-09 09:53 441 查看
intent主要包括隐式意图和显式意图。显式意图通常主要是启动本应用中的Activity之间的数据,而隐式意图则常见于启动系统中的某些特定的动作,比如打电话,发短信,或者是跨应用的Activity启动(如在QQ点击链接地址启动一个浏览器Activity)。

显式意图:调用Intent.setComponent()、Intent.setClass()、Intent.setClassName()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件。
隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。

MainActivity.java

[java] view
plaincopy





package cn.android.intent;  

  

import java.util.Date;  

  

import android.app.Activity;  

import android.content.Intent;  

import android.os.Bundle;  

import android.view.KeyEvent;  

import android.view.View;  

import android.widget.TextView;  

  

public class NewActivity extends Activity {  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        // TODO Auto-generated method stub  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.other);  

        Intent intent = getIntent();  

        //Log.i("NewActivity", intent.getData().getPath());  

        String title = intent.getStringExtra("title");  

        double since = intent.getDoubleExtra("since", 1995.5);  

        TextView textView = (TextView) this.findViewById(R.id.textView);  

        textView.setText("语言:" + title + ",发布时间:" + since + "。");  

    }  

      

    public void closeActivity(View v) {  

        Intent intent = new Intent();  

        intent.putExtra("result", new Date().toString());  

        setResult(501, intent);  

        this.finish();  

    }  

    @Override  

    public boolean onKeyDown(int keyCode, KeyEvent event) {  

        if (keyCode == KeyEvent.KEYCODE_BACK) {  

            closeActivity(null);  

        }  

        return true;  

    }  

}  

NewActivity.java

[java] view
plaincopy





package cn.android.intent;  

  

import java.util.Date;  

  

import android.app.Activity;  

import android.content.Intent;  

import android.os.Bundle;  

import android.view.KeyEvent;  

import android.view.View;  

import android.widget.TextView;  

  

public class NewActivity extends Activity {  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        // TODO Auto-generated method stub  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.other);  

        Intent intent = getIntent();  

        //Log.i("NewActivity", intent.getData().getPath());  

        String title = intent.getStringExtra("title");  

        double since = intent.getDoubleExtra("since", 1995.5);  

        TextView textView = (TextView) this.findViewById(R.id.textView);  

        textView.setText("语言:" + title + ",发布时间:" + since + "。");  

    }  

      

    public void closeActivity(View v) {  

        Intent intent = new Intent();  

        intent.putExtra("result", new Date().toString());  

        setResult(501, intent);  

        this.finish();  

    }  

    @Override  

    public boolean onKeyDown(int keyCode, KeyEvent event) {  

        if (keyCode == KeyEvent.KEYCODE_BACK) {  

            closeActivity(null);  

        }  

        return true;  

    }  

}  

main.xml

[html] view
plaincopy





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

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

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent">  

    <TextView  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:text="@string/hello"  

        android:id="@+id/textView" />  

    <LinearLayout   

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent"  

        android:orientation="horizontal"  

        android:gravity="center_horizontal"  

        android:layout_below="@id/textView">  

        <Button   

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:text="@string/button_display"  

            android:id="@+id/buttonDisplay"  

            android:onClick="openActivityDisplay" />  

        <Button   

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:text="@string/button_hidden"  

            android:onClick="openActivityHidden" />      

    </LinearLayout>  

</RelativeLayout>  

other.xml

[html] view
plaincopy





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

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

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent">  

    <TableRow>  

        <TextView  

            android:layout_width="fill_parent"  

            android:layout_height="wrap_content"  

            android:text="@string/newActivity"  

            android:id="@+id/textView" />  

        <Button   

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:text="@string/button_close"  

            android:onClick="closeActivity" />  

    </TableRow>  

</TableLayout>  

AndroidManifest.xml

[html] view
plaincopy





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

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

    package="cn.android.intent"  

    android:versionCode="1"  

    android:versionName="1.0" >  

  

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

  

    <application  

        android:icon="@drawable/ic_launcher"  

        android:label="@string/app_name" >  

        <activity  

            android:label="@string/app_name"  

            android:name=".MainActivity" >  

            <intent-filter >  

                <action android:name="android.intent.action.MAIN" />  

  

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

            </intent-filter>  

        </activity>  

        <activity android:name=".NewActivity" android:label="@string/newActivity">  

            <intent-filter>  

                <action android:name="cn.java.action.android" />  

                <action android:name="cn.java.action.windowsphone" />  

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

                <category android:name="cn.java.category.intent" />  

                <category android:name="cn.java.category.action" />  

                <data android:scheme="intent" android:host="www.android.com" />  

                <data android:mimeType="image/*" />  

            </intent-filter>  

        </activity>  

    </application>  

  

</manifest>  

strings.xml

[html] view
plaincopy





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

<resources>  

    <string name="hello">Hello World, MainActivity!</string>  

    <string name="newActivity">New Activity!</string>  

    <string name="app_name">Android意图</string>  

    <string name="button_display">打开显示意图</string>  

    <string name="button_hidden">打开隐式意图</string>  

    <string name="button_close">关闭窗口</string>  

</resources>  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: