学习了两个应用之间的activity的调用,和两个应用程序之间的方法调用(运用AIDL实现)
两个应用如下:from应用,to应用(from的activity调用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>