Service面试知识小结
2017-12-31 19:18
369 查看
本博客是对Service基本面试知识的一个小结,该博客复制参考了Hensen_,感谢原文博主的分享,在前人的基础上做了补充,方便后期知识的回顾。
Service在不同Activity中可以获取自身实例,可以方便的对Service进行操作。Thread在不同的Activity中难以获取自身实例,如果Activity被销毁,Thread实例就很难再获取得到。
Service通常应用于后台数据统计、播放音乐、版本更新下载等。
bindService
服务端BindService代码:
客户端 BindServiceActivity 代码:
清单文件配置:
xml布局文件:
打印日志结果:
START_STICKY_COMPATIBILITY,START_STICKY的兼容版本,但不保证服务被终止后一定能重启。
START_STICKY, 程序被异常kill后(服务被重启了,但intent对象被清除了) 。
START_NOT_STICKY,程序被异常kill后(服务没有被重建) 。
START_REDELIVER_INTENT,程序被异常kill后(服务被重启了,并保留了intent对象) 。
onCreate():创建时回调。
onStartCommand():启动时回调,每startService()一次,都会回调一次。
onDestroy():关闭销毁时回调。
bindService
onCreate():创建时回调。
onBind():绑定时回调,多次调用bindService()只会回调一次。
onUnbind():接除绑定时回调。
onDestroy():关闭销毁时回调。
1、Service是什么?
Service是四大组件之一,它可以在后台执行长时间运行操作而没有用户界面的应用组件。2、Service和Thread的区别
Service是安卓中系统的组件,它运行在独立进程的主线程中,不可以执行耗时操作。Thread是程序执行的最小单元,分配CPU的基本单位,可以开启子线程执行耗时操作。Service在不同Activity中可以获取自身实例,可以方便的对Service进行操作。Thread在不同的Activity中难以获取自身实例,如果Activity被销毁,Thread实例就很难再获取得到。
Service通常应用于后台数据统计、播放音乐、版本更新下载等。
3、Service启动方式
startService1.定义一个类继承Service. 2.在Manifest.xml文件中配置该Service。 3.使用Context的startService(intent)方法开启服务。 4.使用Context的stopService(intent)方法关闭服务。 5.该启动方式,app杀死、Activity销毁没有任何影响,服务不会停止销毁。
bindService
1.创建BindService服务端,继承Service并在类中,创建一个实现IBinder接口的实例对象,并提供公共方法给客户端(Activity)调用。 2.从onBinder()回调方法返回该Binder实例。 3.在客户端(Activity)中,从onServiceConnection()回调方法参数中接收Binder,通过Binder对象即可访问Service内部的数据。 4.在manifests中注册BindService, 在客户端中调用 bindService()方法开启绑定Service,调用 unbindService()方法注销解绑Service。 5. 该启动方式依赖于客户端生命周期,当客户端Activity销毁时,没有调用unbindService()方法,Service也会停止销毁。
服务端BindService代码:
package com.example.ling.myreview.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; public class BindService extends Service { private final String TAG = getClass().getName(); private final MyBinder mBinder = new MyBinder(); private final String mMessage = "我是Service的数据哈"; // 通过继承Binder来实现IBinder接口 // service允许客户端通过IBinder对象来访问Service内部数据 public class MyBinder extends Binder { // 获取Service的mMessage数据 public String getServiceMessage() { return mMessage; } } // service被创建时回调 @Override public void onCreate() { super.onCreate(); Log.d(TAG, "Service is create"); } // service被绑定时回调 @Nullable @Override public IBinder onBind(Intent intent) { Log.d(TAG, "Service is bind"); return mBinder; } // service 被启动时回调 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "Service is start"); return super.onStartCommand(intent, flags, startId); } // service 被解绑时回调 @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "Service is unbind"); return super.onUnbind(intent); } // service 被销毁时回调 @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "Service is destroy"); } }
客户端 BindServiceActivity 代码:
package com.example.ling.myreview.service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import com.example.ling.myreview.R; public class BindServiceActivity extends AppCompatActivity { private final String TAG = getClass().getName(); private BindService.MyBinder mBinder = null; private boolean mIsBind = false; // Activity 与 Service 连接状态监听类 private ServiceConnection mConn = new ServiceConnection() { // 连接成功回调方法 @Override public void onServiceConnected(ComponentName name, IBinder service) { // 获取Service onBind方法返回的MyBinder对象。 mBinder = (BindService.MyBinder) service; } // 连接失败出现异常的回调方法 @Override public void onServiceDisconnected(ComponentName name) { mBinder = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bind_service); } // 绑定服务按钮点击事件 public void onClickBindService(View view) { Intent intent = new Intent(this, BindService.class); bindService(intent, mConn, BIND_AUTO_CREATE); mIsBind = true; } // 解绑服务按钮点击事件 public void onClickUnbindService(View view) { if (mIsBind) { unbindService(mConn); mIsBind = false; } } // 获取Service内部数据按钮点击事件 public void onClickGetServiceData(View view) { // 通过MyBinder获取Service内部数据。 if (mBinder != null) { final String message = mBinder.getServiceMessage(); Log.d(TAG, "获取Service的内部数据:message = " + message); } } }
清单文件配置:
<service android:name=".service.BindService" /> <activity android:name=".service.BindServiceActivity" />
xml布局文件:
<?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:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClickBindService" android:text="绑定服务" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClickUnbindService" android:text="解绑服务" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClickGetServiceData" android:text="获取Service里的数据" android:textAllCaps="false" /> </LinearLayout>
打印日志结果:
4、onStartCommand()返回值int值的区别
博文Service中onStartCommand方法返回值的探索。START_STICKY_COMPATIBILITY,START_STICKY的兼容版本,但不保证服务被终止后一定能重启。
START_STICKY, 程序被异常kill后(服务被重启了,但intent对象被清除了) 。
START_NOT_STICKY,程序被异常kill后(服务没有被重建) 。
START_REDELIVER_INTENT,程序被异常kill后(服务被重启了,并保留了intent对象) 。
4、Service生命周期
startServiceonCreate():创建时回调。
onStartCommand():启动时回调,每startService()一次,都会回调一次。
onDestroy():关闭销毁时回调。
bindService
onCreate():创建时回调。
onBind():绑定时回调,多次调用bindService()只会回调一次。
onUnbind():接除绑定时回调。
onDestroy():关闭销毁时回调。
相关文章推荐
- IntentService面试知识小结
- BrodcastReceiver面试知识小结
- AsyncTask面试知识小结
- 2015腾讯应用开发面试基础知识小结
- ListView面试知识小结
- andriod编程--service 基础知识小结
- Git版本控制面试知识小结
- Handler面试知识小结
- Android Proguard代码混淆面试知识小结
- Android构建面试知识小结
- android内存泄漏面试知识小结
- android冷启动与热启动面试知识小结
- WebView面试知识小结
- 面试复习知识小结
- Android UI卡顿面试知识小结
- oracle知识小结和javaweb面试常见的问题
- 面试知识小结
- Android Binder 面试知识小结
- HandlerThread面试知识小结
- Microservice 和 Docker 知识小结