android一个应用的activity调用另一个应用的activity

学习了两个应用之间的activity的调用,和两个应用程序之间的方法调用(运用AIDL实现)

两个应用如下:from应用,to应用(fromactivity调用to应用的activity

实现方式:

1.在from的清单文件里声明要调用的to应用的activity

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.from.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="com.example.to.ToActivity" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" >
                </action>

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

2.在activity中调用的代码如下

             ComponentName componetName = new ComponentName(  
				//这个是另外一个应用程序的包名  
				"com.example.to",  
				//这个参数是要启动的Activity  
				"com.example.to.ToActivity");  
		try {  
			Intent intent = new Intent();  
			intent.setAction("android.intent.action.VIEW");
//			Bundle bundle = new Bundle();
//			bundle.putCharSequenceArray("val",new String[]{"111","222","333","444"});
//			intent.putExtras(bundle);//绑定bundle数据
//			intent.setComponent(componetName); 
			startActivity(intent);  
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "可以在这里提示用户没有找到应用程序,或者是做其他的操作!", 0).show();  
			Log.v("go to apk error","------>"+e.toString());
		}  

因为我们是从from应用调用to应用,所以还需要在to应用中将需要被调用的activity设置属性,如下:

  <activity android:name="com.example.to.ToActivity"
            android:exported="true">
            
   </activity>




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。