Android隐式意图激活组件
1.通过清单文件指明:
<action>
<category>
<data> 通过URI描述
<activity android:name=".OtherActivity">
<intent-filter >
<action android:name="com.example.zx"/>
<category android:name="com.example.category.java"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="xxx" android:host="www.xxx.com" android:path="/xxx"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
2.在MainActivity中设置intent的上述三个属性:
Intent intent = new Intent();//隐式意图激活Activity
intent.setAction("com.example.zx");
intent.addCategory("com.example.category.java");
intent.setDataAndType(Uri.parse("xxx://www.xxx.com/xxx"), "image/jpeg");
startActivity(intent);//方法内部为Intent添加了android.intent.category.DEFAULT类别
规律:在没有设置数据参数的前提下,只要Intent中的Action和Category都出现在Intent-Filter中,就能与之匹配,否则匹配是被。
3.附录AndroidManifest.xml & IntentActivity.java
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mulactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
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.mulactivity.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>
<intent-filter >
<action android:name="com.example.zx"/>
<category android:name="com.example.category.java"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="xxx" android:host="www.xxx.com" android:path="/xxx"/>
<data android:mimeType="image/*"/>
</intent-filter>
<activity
android:name="com.example.mulactivity.OtherActivity"
android:label="@string/title"
/>
</application>
</manifest>
IntentActivity.java
public class IntentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void openActivity(View v){
Intent intent = new Intent();//隐式意图激活Activity
intent.setAction("com.example.zx");
intent.addCategory("com.example.category.java");
intent.setDataAndType(Uri.parse("xxx://www.xxx.com/xxx"), "image/jpeg");
startActivity(intent);//方法内部为Intent添加了android.intent.category.DEFAULT类别
}
}