如何实现跨应用启动Service

来源:互联网 发布:金冠网络中介平台 编辑:程序博客网 时间:2024/06/17 15:10

跨应用启动Service需要注意的地方有2点:

1.目标service需要声明exported=true 的属性,表示允许其他应用访问该服务.
2.android5.0之前是可以通过设置隐式意图来跨应用打开Service的,5.0之后就必须要通过显示意图来开启Service.
如何创建跨应用的显示意图呢?

通过Intent的setComponent方法,可以传递一个ComponentName对象,该对象有一个接受2个参数的构造方法,第一参数传递目标Service所在的包名,第二个参数传递目标Service的完整类名.

Demo的结构:


本Demo是通过app2工程的App2Activity打开app工程的AppService服务.同时实现跨应用传递数据的功能

AppService代码:

[java] view plain copy
  1. public class AppService extends Service {  
  2.     public AppService() {  
  3.     }  
  4.   
  5.     @Override  
  6.     public IBinder onBind(Intent intent) {  
  7.         return null;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void onCreate() {  
  12.         super.onCreate();  
  13.         Log.i("AppService""onCreate");  
  14.     }  
  15.   
  16.     @Override  
  17.     public int onStartCommand(Intent intent, int flags, int startId) {  
  18.         if (null != intent) {  
  19.             Log.i("AppService""onStartCommand接收到的数据是:" + intent.getStringExtra("data"));  
  20.         }  
  21.         return super.onStartCommand(intent, flags, startId);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onDestroy() {  
  26.         super.onDestroy();  
  27.         Log.i("AppService""onDestroy");  
  28.     }  
  29. }  

清单文件配置:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.mchenys.aidlservicedemo" >  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:supportsRtl="true"  
  10.         android:theme="@style/AppTheme" >  
  11.         <activity android:name=".MainActivity" >  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.   
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.   
  19.         <service  
  20.             android:name=".AppService"  
  21.             android:enabled="true"  
  22.             android:exported="true" />  
  23.     </application>  
  24.   
  25. </manifest>  

App2Activity代码:

[java] view plain copy
  1. public class App2Activity extends AppCompatActivity {  
  2.   
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_app2);  
  8.   
  9.         //注意:android 5.0之前是可以通过隐式意图打开其他app的服务的,5.0之后只能通过显式意图来打开.  
  10.         final Intent intent = new Intent();  
  11.         //ComponentName的参数1:目标app的包名,参数2:目标app的Service完整类名  
  12.         intent.setComponent(new ComponentName("com.example.mchenys.aidlservicedemo""com.example.mchenys.aidlservicedemo.AppService"));  
  13.         //打开目标AppService  
  14.         findViewById(R.id.id_btn_start).setOnClickListener(new View.OnClickListener() {  
  15.             @Override  
  16.             public void onClick(View v) {  
  17.                 //设置要传送的数据  
  18.                 intent.putExtra("data""Hello AppService,I am App2Activity");  
  19.                 startService(intent);  
  20.             }  
  21.         });  
  22.         //关闭目标AppService  
  23.         findViewById(R.id.id_btn_stop).setOnClickListener(new View.OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 stopService(intent);  
  27.             }  
  28.         });  
  29.     }  
  30. }  
布局文件的效果就是2个Button,分别用于开启目标服务和关闭目标服务

效果图:
点击启动APPSERVICE后,查看控制台的log.
11-01 10:58:57.691 29359-29359/? I/AppService: onCreate
11-01 10:58:57.692 29359-29359/? I/AppService: onStartCommand接收到的数据是:Hello AppService,I am App2Activity

由控制台可以看到目标Service被开启了.同时在onStartCommand方法中接收到了我们通过显示意图传递过去的参数.
当点击停止APPSERVICE后,再查看控制台log:
11-01 11:00:38.350 29359-29359/? I/AppService: onDestroy
可以看到目标Service的onDestory方法被调用了,说明跨应用关闭服务也成功了.
0 0