1、显式启动


Service端

需要声明android:exported="true",这样别的APP才能访问。

            android:exported= "true">


客户端:

需要知道Sercvice的名字以及其Application所在的包名。

public static final String NAME_GESTURE_DETECTOR_REMOTE_SERVICE = "com.easyliu.watchgesture.service.GestureDetectorService" ;
public static final String PACKAGE_GESTURE_DETECTOR_REMOTE_SERVICE = "com.easyliu.watchgestureremoteservice" ;
//启动服务
Intent startIntent = new Intent ();
ComponentName componentName = new ComponentName(
                    PACKAGE_GESTURE_DETECTOR_REMOTE_SERVICE ,
                    NAME_GESTURE_DETECTOR_REMOTE_SERVICE);
          startIntent .setComponent (componentName );
           startService( startIntent) ;
//绑定服务
Intent startIntent = new Intent ();
          ComponentName componentName = new ComponentName(
                    PACKAGE_GESTURE_DETECTOR_REMOTE_SERVICE ,
                    NAME_GESTURE_DETECTOR_REMOTE_SERVICE);
          startIntent .setComponent (componentName );
           bindService( startIntent, mConnection, Context.BIND_AUTO_CREATE) ;


2、隐式启动


Service端:

需要设置一个Action,我们可以把Action的名字设置成Service的全路径名字。在这种情况下android:exported默认为true。

 
             
               
           
 


客户端:

需要同时设置Action和package,记住之设置Action会报错,必须同时设置其包名。

Intent startIntent = new Intent ();

startIntent .setAction ("com.easyliu.watchgesture.service.GestureDetectorService" );

startIntent .setPackage (PACKAGE_GESTURE_DETECTOR_REMOTE_SERVICE);

startService( startIntent) ;


Android官网推荐用显式的方式启动Service。

关于Service详细介绍,请看:http://blog.csdn.net/liuyi1207164339/article/details/51669971















更多相关文章

  1. Android(安卓)Service 服务(三)—— bindService与remoteService
  2. 如何在mac book 上安装Android(安卓)studio 开发工具
  3. Android(安卓)DataBinding 双向数据绑定、事件绑定、使用类方法
  4. Android使用webservice客户端实例
  5. 【疯狂Android讲义】第13章网络应用(一)
  6. Android集成常见问题
  7. Android深入浅出之Surface[1]
  8. Android开发之实现两个手机之间的通讯
  9. JIN学习一、Android使用已有C/C++代码、第三方SO库的方法

随机推荐

  1. 2.[WP Developer体验Andriod开发]Andriod
  2. android 内存泄露原因以及排查和解决方案
  3. android apk自动升级实现
  4. Android(安卓)Mp3格式录音,含有暂停,计时功
  5. Android获取手机号码(问题)
  6. Android(安卓)UI设计之自定义SwitchButto
  7. Android(安卓)& 百度地图(1)显示地图
  8. 我是如何自学 Android(安卓)的?
  9. Android(安卓)进阶 APP优化 包体积优化
  10. 【译】利用多线程提高程序性能(for Androi