现在的位置: 首页 > 综合 > 正文

android intent深入解析

2017年04月15日 ⁄ 综合 ⁄ 共 1906字 ⁄ 字号 评论关闭

一.Intent的显示调用:

//    1 intent.setClass(this, OtherActivity.class);
//    2	intent.setClassName(this, "com.zizhu.activitys.OtherActivity");
//    3	intent.setComponent(new ComponentName(this, OtherActivity.class));
//    4	Intent intent = new Intent(this, OtherActivity.class);
    	intent.setClassName("com.zizhu.activitys", "com.zizhu.activitys.OtherActivity");//这个方法可以激活其他应用的Activity
    	

二.intent的隐式调用:

 public void openOtherActivity(View view){
    	/**
    	 * 只要Intent中的action和category都出现在 意图过滤器中,都能与之匹配,否则匹配不成功
    	 * 另外,如果manifest文件中配置了action, category, data等内容,intent必须都要匹配才能激活,如果只匹配一部分是不能激活的
    	 */
    	Intent intent = new Intent();
    	intent.setAction("com.zizhu.other");
    	intent.addCategory("com.zizhu.category.android");
    	intent.addCategory("android.intent.category.DEFAULT");
    	intent.setDataAndType(Uri.parse("zizhu://www.zizhu.com"), "image/jpeg");//如果既设置了mineType,也设置了data,必须用这个方法
//    	intent.setData(Uri.parse("zizhu://www.zizhu.com"));
//    	intent.setType("image/jpeg");//这个方法会自动清除设置了data的内容
    	startActivity(intent);//这个方法会自动添加category为 andriod.intent.category.DEFAULT,所以在配置文件中需要添加这个category
    }

manifest配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zizhu.intent"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        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>
        
        <activity
            android:name=".OtherActivity"
            android:label="@string/other" >
            <intent-filter>
                <action android:name="com.zizhu.other" />
                <category android:name="com.zizhu.category.android" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="zizhu" android:host="www.zizhu.com" />
                <data android:mimeType="image/*"></data>
            </intent-filter>
        </activity>
    </application>

</manifest>

抱歉!评论已关闭.