安卓第十三天笔记-服务(Service),安卓第十三天
安卓第十三天笔记-服务(Service)
Servcie服务
1.服务概念
服务
- windows
服务没有界面,一直运行在后台, 运行在独立的一个进程里面
- android
服务没有界面,一直运行在后台,默认是运行当前的应用程序进程里面。
2.建立服务
-
建立一个类继承Service类
public class ServiceDemo extends Service {
-
在清单文件中注册service
<service android:name="com.ithiema.servicequick.servcie.ServiceDemo"></service>
3.生命周期
只会在开启服务时初始化一次
@Override public void onCreate() { //只会在开启服务时初始化一次 super.onCreate(); }
每次开启服务都会执行调用
@Override public int onStartCommand(Intent intent, int flags, int startId) { //每次开启服务都会执行调用 return super.onStartCommand(intent, flags, startId); }
停止服务时,只执行一次
public void onDestroy() { //停止服务时,只执行一次 super.onDestroy(); }
- 完整生命周期
onCreate -- onStartCommand--onDestroy
- 启动多次服务
onCreate方法只会执行一次, 但是onStartCommand执行多次
- 多次停止服务
只会执行一次onDestroy方法。
4.进程
Foreground process
前台进程: 当前与用户进行交互的应用所处的进程
- Visible process
可见进程: 应用程序不能交互,但是界面可见。 有点类似activity生命周期的onPause
- Service process
服务进程: 应用程序里面运行着一个服务
- Background process
后台进程: 应用程序被最小化了(home)
- Empty process
空进程:应用程序里面没有任何活动的组件(activity \ service)
前台进程 > 可见进程 > 服务进程 > 后台进程 > 空进程
5.开启与停止服务
public void start(View v){ startService(new Intent(this , ServiceDemo.class)); } public void stop(View v){ stopService(new Intent(this , ServiceDemo.class)); }
6.为什么要使用服务
场景: 1. 在后台检测设备接入状况 2. 在后台执行联网数据请求(类似股票软件) 3. 音乐播放器
7.绑定服务第一种
Extending the Binder class 第一种
1 先写一个继承Service类
2.在Service中写一个public类继承Binder,在这个类中写个方法返回具服务类的对象实例
3.在onBind 中返回 这个内部类的实例
4.在service中写几个public的方法提供给子类调用必须为public的
/** * Created by 刘楠 on 2016-02-28 14:19. */ public class LocalService extends Service { private static final String TAG = "LocalService"; //声明绑定的类 private LocalBinder mBinder = new LocalBinder(); //给客户端使用 private final Random mGenerator = new Random(); /* 第一种继承Binder类 */ public class LocalBinder extends Binder { public LocalService getServcie() { Log.d(TAG, "==getServcie=="); return LocalService.this; } } @Nullable @Override public IBinder onBind(Intent intent) { Log.d(TAG, "==IBinder=="); return mBinder; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "==onCreate=="); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "==onStartCommand=="); return super.onStartCommand(intent, flags, startId); } /** * 返回一个随机数 * * @return */ public int getRandom() { Log.d(TAG, "==getRandom=="); return mGenerator.nextInt(500); } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "==onUnbind=="); return super.onUnbind(intent); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "==onDestroy=="); } }
5.在Manifest.xml中注册service
<service android:name=".serivice.LocalService"/>
6.在客户端的Activity中绑定service bindService(intnet,ServiceConnection,Context.BINDAUTOCREATE)
7.写一个类实现ServiceConnection接口,在方法中完成获取Service类的实例
8.调用服务中的方法
9.解除绑定
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:onClick="bind1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="绑定服务1"/> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:onClick="show" android:layout_width="match_parent"