我們知道,可以通過隱式Intent來跨應用啓動Activity,通過配置
intent-filter中的Action:
startActivity(new Intent("aty_B"));
對於跨應用啓動Service的情況,Android 5.0之後就不允許通過隱式啓動了。
serviceIntent = new Intent();
serviceIntent.setComponent(new ComponentName("com.jackie.startanotherapp", "com.jackie.startanotherapp.AppService"));
startService(serviceIntent);
這裏指定了包名,類名,因此是一個顯式啓動,合符要求。
啓動/停止anotherApp中的按鈕,主意Logcat要切換到包含Service的那個app,
10-20 22:17:46.231 26358-26358/com.jackie.startanotherapp V/jackie: Service created
10-20 22:17:50.376 26358-26358/com.jackie.startanotherapp V/jackie: Service destroyed