android调用其他apk的activity
<img src="https://img-blog.csdn.net/20160322114625025" alt="" />启动另一个apk的工程
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("nafio","start load1"); ComponentName componetName = new ComponentName( //这个是另外一个应用程序的包名 "com.example.t2", //这个参数是要启动的Activity "com.a.cc.TActivity"); try { Intent intent = new Intent(); intent.setComponent(componetName); startActivity(intent); } catch (Exception e) { Toast.makeText(getApplicationContext(), "可以在这里提示用户没有找到应用程序,或者是做其他的操作!", 0).show(); Log.e("nafio",e.getMessage()); } Log.e("nafio","end load"); } }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.n.tlaunchotherapk" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:configChanges="keyboardHidden|navigation" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
被启动的工程,包名为com.example.t2 ,因为TActivity为非Action.Main,要想外部访问,要加android:exported="true"
package com.example.t2; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("nafio","T2---------------"); } }
package com.a.cc; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class TActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("nafio","TActivity!!!!!---------------"); } }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.t2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:configChanges="keyboardHidden|navigation" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.a.cc.TActivity" android:configChanges="keyboardHidden|navigation" android:label="@string/app_name" <span style="color:#ff0000;">android:exported="true"</span> android:screenOrientation="portrait"> </activity> </application> </manifest>