Android-Service
2015-12-22 16:09
465 查看
Android提供了两种Service的方式 startService 和bindService
区别:bindService和组件关联 如果对应的组件关闭 Service destroy
绑定服务调用的函数是bindService 启动服务调用的是onStartcommand onCreate方法只在创建的时候调用 如果服务正在运行 onCreate不会调用
onStartCommand 有返回值
START_NOT_STICKY 不会重新创建
START_STICKY 总是重新创建 过一段时间后 由系统决定
START_REDELIVER_INTENT 适用于下载 马上恢复
声明一个Servic
以后AIDL中有一个很重要的属性那就是exported 如果为true才能被其他应用程序调用
1.StartService(启动服务)
这个比较简单 继承Service 或者intentService(这个基本没人用 Android为了方便我们封装了一个Service的子类给我们使用) 然后重写里面的方法
启动服务
这样启动一般是没有结果回传的 但是官网好像说可以通过为一个Broadcast创建PendingIntent 然后用intent传过去
也就是Service想和组件通信 就必须用Broadcast
启动服务 然后可以发送Toast Notifications or
Status Bar Notifications
这个两个到底是什么鬼呢?就是说你可以通过Toast和Notification来提示用户 说服务启动了
可以声明一个前台进程 然后你的Service就不会随意系统回收了
语法如下:
2.绑定服务bindService
这个直接上来就是AIDL
服务器:1)定义aidl接口 2)然后定义Service 3)返回这个aidl
Create the .aidl file
This file defines the programming interface with method signatures.
Implement the interface
The Android SDK tools generate an interface in the Java programming language, based on your
Expose the interface to clients
Implement a
哈哈 终于把今天那个问题解决了都不知道是怎么回事
区别:bindService和组件关联 如果对应的组件关闭 Service destroy
绑定服务调用的函数是bindService 启动服务调用的是onStartcommand onCreate方法只在创建的时候调用 如果服务正在运行 onCreate不会调用
onStartCommand 有返回值
START_NOT_STICKY 不会重新创建
START_STICKY 总是重新创建 过一段时间后 由系统决定
START_REDELIVER_INTENT 适用于下载 马上恢复
声明一个Servic
<manifest ... > ... <application ... > <service android:name=".ExampleService" /> ... </application> </manifest>
以后AIDL中有一个很重要的属性那就是exported 如果为true才能被其他应用程序调用
1.StartService(启动服务)
这个比较简单 继承Service 或者intentService(这个基本没人用 Android为了方便我们封装了一个Service的子类给我们使用) 然后重写里面的方法
启动服务
Intent intent = new Intent(this, HelloService.class); startService(intent);
这样启动一般是没有结果回传的 但是官网好像说可以通过为一个Broadcast创建PendingIntent 然后用intent传过去
也就是Service想和组件通信 就必须用Broadcast
启动服务 然后可以发送Toast Notifications or
Status Bar Notifications
这个两个到底是什么鬼呢?就是说你可以通过Toast和Notification来提示用户 说服务启动了
可以声明一个前台进程 然后你的Service就不会随意系统回收了
语法如下:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), System.currentTimeMillis()); Intent notificationIntent = new Intent(this, ExampleActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, getText(R.string.notification_title), getText(R.string.notification_message), pendingIntent); startForeground(ONGOING_NOTIFICATION_ID, notification);
2.绑定服务bindService
这个直接上来就是AIDL
服务器:1)定义aidl接口 2)然后定义Service 3)返回这个aidl
Create the .aidl file
This file defines the programming interface with method signatures.
Implement the interface
The Android SDK tools generate an interface in the Java programming language, based on your
.aidlfile. This interface has an inner abstract class named
Stubthat extends
Binderand implements methods from your AIDL interface. You must extend the
Stubclass and implement the methods.
Expose the interface to clients
Implement a
Serviceand override
onBind()to return your implementation of the
Stubclass.
哈哈 终于把今天那个问题解决了都不知道是怎么回事
相关文章推荐
- Android开发常用
- android studio gradlew编译时proguardRelease报错
- Android中获取手机屏幕大小的方法
- Android Drawable中shape详解
- Android日期/时间选择器(工具类)
- android显式启动与隐式启动
- android- ScrollView与ListView冲突(最后一条Item显示不全)
- Android之样式和主题
- 开发android盒子,如何获取Android IP地址
- android中点击空白处隐藏软键盘
- Android编程之TabWidget选项卡用法实例分析
- Android开发_关于中英文切换
- 1.Android入门学习
- Android使用FTP实现断点续传
- Android Studio使用外部jar包
- android图片大小按比例缩放
- Android初学习 - Activity的四种启动模式(回退栈)
- Android初学习 - 消息循环机制 Looper Handler类分析
- Android应用程序签名过程和解析过程分析
- Android初学习 - Activity之间的跳转