歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 跨應用調用Activity

Android 跨應用調用Activity

日期:2017/3/1 10:10:13   编辑:Linux編程

如何調用另外一個app應用的activity或者service,本文提供一個驗證可行的方法。

調用方法:

  1. Intent intent=new Intent("youActionName");
  2. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  3. intent.addCategory(Intent.CATEGORY_DEFAULT);
  4. intent.putExtra("type",inType); //if needed
  5. ComponentName cn=new ComponentName("applicationPackageName","packagename+classname");
  6. intent.setComponent(cn);
  7. startActivity(intent);

在被調用的App裡面需要定義 class (activity 或 service)屬性和filter。需要明確的幾點

如果不是action.Main,則需要主動申明Android:exported="true",允許外部訪問

action name 要一致

category name要一致,如果調用的地方沒有明確聲明,被調用的地方要聲明DEFAULT

  1. <activity android:name=".pbap.BluetoothPbapLuancherActivity"
  2. android:label="Bluetooth"
  3. android:exported="true"
  4. android:process="@string/process">
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN" />
  7. <category android:name="android.intent.category.DEFAULT" />
  8. </intent-filter>
  9. </activity>
Copyright © Linux教程網 All Rights Reserved