1、MainActivity.java
package com.ex;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
private static final String ACTION1="com.ex.TEST_ACTION1";
private static final String ACTION2="com.ex.TEST_ACTION2";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//测试action 在AndroidManifest里添加了一句上面的ACTION进去的语句
/*Intent intent = new Intent();
intent.setAction(ACTION2);
startActivity(intent);*/
//测试Data,在AndroidManifest里添加了一句data的语句
/*Intent intent = new Intent();
Uri data = Uri.parse("content://com.ex/abc");
intent.setData(data);
startActivity(intent);*/
//测试Data,在AndroidManifest加ACTION和Data
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("http://www.google.com"));
}
});
}
}
2、TestActivity,java
package com.ex;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
}
3、两个xml比较简单点 main.xml 和 main1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试Intent Fileter"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试Intent Fileter"
/>
</LinearLayout>
4、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex"
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>
<activity android:name=".TestActivity" >
<intent-filter >
<action android:name="com.ex.TEST_ACTION1"/>
<action android:name="com.ex.TEST_ACTION2"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="http" android:path="www.google.com"/>
<data android:scheme="content" android:path="com.ex/abc" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>