06_Intent和IntentFilter
阅读原文时间:2023年07月08日阅读:1

  Intent是同一个或不同的应用中的组件之间的消息传递的媒介,是一个将要执行动作的抽象描述,一般来说是作为参数来使用。

  Intent描述要启动的基本组件;

  IntentFilter对组件进行过滤。

  下面的代码通过Intent,具有打开Activity、打开图片、拨打10086和打开网页的功能。

package com.example.intentdemo;

import java.io.File;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity\_main);

    findViewById(R.id.btnStartAty1).setOnClickListener(new View.OnClickListener() {

        @Override  
        public void onClick(View v) {

            //显式Intent  
            //Intent i = new Intent();  
            //i.setComponent(new ComponentName("com.example.intentdemo", "com.example.intentdemo.Aty1"));

            //通过一个action来启动,由操作系统来启动  
            //隐式Intent  
            Intent i = new Intent("com.example.intentdemo.intent.action.Ayt1");  
            startActivity(i);

        }

    });

    findViewById(R.id.btnOpenImage).setOnClickListener(new View.OnClickListener() {

        @Override  
        public void onClick(View v) {  
            File f = new File("/storage/emulated/0/Pictures/Screenshots/Screenshot\_2014-08-30-08-08-10.png");  
            Intent i = new Intent(Intent.ACTION\_VIEW);  
            i.setDataAndType(Uri.fromFile(f), "image/\*");  
            startActivity(i);

        }  
    });

    findViewById(R.id.btnDel10086).setOnClickListener(new View.OnClickListener() {

        @Override  
        public void onClick(View v) {  
            Intent i = new Intent(Intent.ACTION\_VIEW);  
            i.setData(Uri.parse("tel:10086"));  
            startActivity(i);  
        }  
    });

    findViewById(R.id.btnOpenBaidu).setOnClickListener(new View.OnClickListener() {

        @Override  
        public void onClick(View v) {  
            Intent i = new Intent(Intent.ACTION\_VIEW, Uri.parse("http://www.baidu.com"));  
            startActivity(i);  
        }  
    });  
}

}

Manifest文件:


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

<application  
    android:allowBackup="true"  
    android:icon="@drawable/ic\_launcher"  
    android:label="@string/app\_name"  
    android:theme="@style/AppTheme" >  
    <activity  
        android:name="com.example.intentdemo.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="Aty1">  
        <intent-filter>  
            <category android:name="android.intent.category.DEFAULT" /><action android:name="com.example.intentdemo.intent.action.Ayt1" />

        </intent-filter>  
    </activity>

    <activity android:name="ImageViewer">

        <intent-filter >

            <action android:name="android.intent.action.view"/>  
            <category android:name="android.intent.category.DEFAULT"/>  
            <data android:mimeType="image/\*" android:scheme="file"/>

        </intent-filter>

    </activity>  
</application>