兩個獨立app相互啟動
在android的應用開發時有這樣一種場景。A程式需要啟動B程式。這裡介紹使用到android的intent來啟動的兩種方式。
1、intent和ComponentName啟動,具體程式碼如下:
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.xx.xx.xx", "com.xx.xx..main.MainActivity"); //com.xx.xx.xx為app的包名
intent.setComponent(comp);
startActivity(intent);
程式碼很簡單,就不詳細解釋。
2、使用intent的隱式方式啟動。
使用隱式啟動時,需要在AndroidManifest.xml檔案中對應被啟動的activity中新增intent-filter,利用intent-filter來啟動。
<activity
android:name="com.xx.xx.xx.main.MainActivity"
android:configChanges="locale"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="com.xx.xx.xx.main.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
重點需要說明的是,一定要新增 <category android:name="android.intent.category.DEFAULT"/> ,否則隱式啟動有問題。
接著就是通過intent進行啟動了,程式碼如下:
Intent intent = new Intent();
intent.setAction("com.xx.xx.xx.main.MainActivity");
startActivity(intent);
以上就是兩種啟動的方式。