使用Intent filter来实现不同应用中Activity的相互跳转

时间:2022-12-02 17:09:25

点击A应用中的Activity,就跳转到B应用中的Activity,这种方式可以实现从一个应用的Activity 跳转到另一个应用的Activity,它们之间还可以传递数据。


效果图:

这是_08StartActivity项目下的_08StartActivityActivity

使用Intent filter来实现不同应用中Activity的相互跳转

这是_08StartActivity_Other项目下的SendActivity

使用Intent filter来实现不同应用中Activity的相互跳转

点击_08StartActivity项目下的_08StartActivityActivity,程序会跳转到_08StartActivity_Other项目下的SendActivity,并且显示hello haozi。


代码如下:

_08StartActivity项目的manifest 文件

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="._08StartActivityActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

_08StartActivity项目的main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


_08StartActivity项目的_08StartActivityActivity

public class _08StartActivityActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	
    	if(event.getAction() == MotionEvent.ACTION_UP){
        	Intent intent = new Intent(Intent.ACTION_SEND);
        	intent.putExtra(Intent.EXTRA_EMAIL, "hello haozi");
        	startActivity(intent);    		
    	}
    	return super.onTouchEvent(event);
    }
}


_08StartActivity_Other项目下的manifest 文件

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="._08StartActivity_OtherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- 关键是这个SendActivity -->
      	<activity 
      	    android:name=".SendActivity"
            android:label="i am send.">
            <intent-filter >
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>


_08StartActivity_Other项目下的send.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/textView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="i am send!"/>

</LinearLayout>

_08StartActivity_Other项目下的SendActivity

public class SendActivity extends Activity {
	
	private TextView textView01;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send);
        
        this.textView01 = (TextView) this.findViewById(R.id.textView01);
        if(getIntent() != null){
        	String info = getIntent().getExtras().get(Intent.EXTRA_EMAIL).toString();
        	textView01.setText(info);
        }
    }
}

总结:An <activity> element can also specify various intent filters—using the<intent-filter> element—in order to declare how other application components may activate it.


demo下载链接地址:http://download.csdn.net/detail/hello_haozi/4290890