前言:
启动另一个app的activity和service其实是一样的,区别在于startActivity(intent)还是startService(intent)而已;所以下面案例以启动另一个app的服务为例;
第一种方式:
通过app包名和全路径类名
Intent intent = new Intent(Intent.ACTION_VIEW);
String packageName = "com.ang.chapter_2_service"; //另一个app的包名
String className = "com.ang.chapter_2.poolBinder.BinderPoolService"; //另一个app要启动的组件的全路径名
intent.setClassName(packageName, className);
startService(intent);//或者bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 都能启动
注意:另一个app中的Service别忘了在清单文件中注册
如果设置了service的android:exported="true"属性,千万要为true ;如果为false,意味着不允许其他应用启动此service;
第二种方式:
通过ComponentName这个类启动
ComponentName componetName = new ComponentName(
"com.ang.chapter_2_service", //这个参数是另外一个app的包名
"