当调用者通过unbindService()函数取消绑定Service时,onUnbind()函数将被调用。如果onUnbind()函数返回true,则表示重新绑定服务时,onRebind ()函数将被调用。
startService样例
- 1.创建StartService.java继承自Service类,重写onCreate()方法、onStartCommand()方法、onBind()方法、onDestroy()方法,其代码如下:
public class StartService extends Service {
@Override
public void onCreate() {
super.onCreate();
MLog.e(getClass().getName(), “onCreate”);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
MLog.e(getClass().getName(), “onStartCommand”);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
MLog.e(getClass().getName(), “onDestroy”);
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
- 2.创建ServiceActivity.java和配套的activity_service.xml文件,其代码如下:
public class ServiceActivity extends ActivityBase {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
Intent intentStart = new Intent(ServiceActivity.this, StartService.class);
findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(intentStart);
}
});
findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intentStart);
}
});
}
}
- 配套的activity_service.xml文件
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id=“@+id/ll_bg”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:background=“@color/color_666666”>
<Button
android:id=“@+id/btn_start”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“start启动服务”/>
<Button
android:id=“@+id/btn_stop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“start停止服务”/>
- 3.添加Service组件声明,在AndroidManifest.xml文件中声明一个Service组件,其代码如下:
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.scc.demo”>
<application
…>
<activity
…>
…
- 运行结果
07-07 16:41:11.474 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonCreate
07-07 16:41:11.481 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonStart
07-07 16:41:11.482 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonResume
07-07 16:41:13.313 E/-SCC-com.scc.demo.service.StartService: onCreate
07-07 16:41:13.334 E/-SCC-com.scc.demo.service.StartService: onStartCommand
07-07 16:41:16.705 E/-SCC-com.scc.demo.service.StartService: onDestroy
bindService样例
- 1.创建BindService.java继承自Service类,重写onCreate()方法、onBind()方法、onUnbind()方法、onDestroy()方法,实现本地通知栏显示,其代码如下:
public class BindService extends Service {
//声明IBinder接口的一个接口变量mBinder
public final IBinder mBinder = new LocalBinder();
private NotificationManager mNM;
private int NOTIFICATION = R.string.local_service_started;
//LocalBinder是继承Binder的一个内部类
public class LocalBinder extends Binder {
public BindService getService() {
return BindService.this;
}
}
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
MLog.e(getClass().getName(), “onCreate”);
showNotification();
}
@Override
public void onDestroy() {
MLog.e(getClass().getName(), “onDestroy”);
mNM.cancel(NOTIFICATION);
Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
MLog.e(getClass().getName(), “onBind”);
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
MLog.e(getClass().getName(), “onUnbind”);
return super.onUnbind(intent);
}
private void showNotification() {
CharSequence text = getText(R.string.local_service_started);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ServiceActivity.class), 0);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(text)
.setWhen(System.currentTimeMillis())
.setContentTitle(getText(R.string.local_service_label))
.setContentText(text)
.setContentIntent(contentIntent)
.build();
mNM.notify(NOTIFICATION, notification);
MLog.e(getClass().getName(), “通知栏已出”);
}
}
- 2.创建ServiceActivity.java和配套的activity_service.xml文件,其代码如下:
public class ServiceActivity extends ActivityBase {
private BindService bindService;
private boolean isBind = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
findViewById(R.id.btn_bind).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isBind) {
Intent intentBind = new Intent(ServiceActivity.this, BindService.class);
bindService(intentBind, serviceConnection, Context.BIND_AUTO_CREATE);
isBind = true;
}
}
});
findViewById(R.id.btn_unbing).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isBind) {
isBind = false;
unbindService(serviceConnection);
bindService = null;
}
}
});
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MLog.e(getClass().getName(), “onServiceConnected”);
bindService = ((BindService.LocalBinder) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
MLog.e(getClass().getName(), “onServiceDisconnected”);
bindService = null;
}
};
}
- 配套的activity_service.xml文件
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id=“@+id/ll_bg”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:background=“@color/color_666666”>
<Button
android:id=“@+id/btn_bind”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“bind服务绑定”/>
<Button
android:id=“@+id/btn_unbing”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“bind解除绑定”/>
- 3.添加Service组件声明,在AndroidManifest.xml文件中声明一个Service组件,其代码如下:
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.scc.demo”>
<application
…>
<activity
…>
…
- 4运行结果
07-07 17:00:04.309 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonCreate
07-07 17:00:04.350 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonStart
07-07 17:00:04.350 E/-SCC-: com.scc.demo.actvitiy.ServiceActivityonResume
07-07 17:00:10.088 E/-SCC-com.scc.demo.service.BindService: onCreate
07-07 17:00:10.120 E/-SCC-com.scc.demo.service.BindService: 通知栏已出
07-07 17:00:10.145 E/-SCC-com.scc.demo.service.BindService: onBind
07-07 17:00:10.164 E/-SCC-com.scc.demo.actvitiy.ServiceActivity$5: onServiceConnected
07-07 17:00:39.111 E/-SCC-com.scc.demo.service.BindService: onUnbind
07-07 17:00:39.134 E/-SCC-com.scc.demo.service.BindService: onDestroy
三、Service生命周期
「onBind()」 是Service必须实现的方法,返回的IBinder对象相当于Service组件的代理对象,Service允许其他程序组件通过IBinder对象来访问Service内部数据,这样即可实现其他程序组件与Service之间的通信。
startService启动的生命周期
「onCreate()」 当Service第一次被创建时,由系统调用。
「onStartCommand()」 当startService方法启动Service时,该方法被调用。
「onDestroy()」 当Service不再使用时,由系统调用。
注意:一个startService只会创建一次,销毁一次,但可以开始多次,因此,onCreate()和onDestroy()方法只会被调用一次,而onStart()方法会被调用多次。
bindService启动的生命周期
「onCreate()」 当Service被创建时,由系统调用。
「onBind()」 当bindService方法启动Service时,该方法被调用。
「onUnbind()」 当unbindService方法解除绑定时,该方法被调用。
「onDestroy()」 当Service不再使用时,由系统调用。
注意:一个bindService可以创建多次,销毁多次,重复使用。
四、Service和Thread的区别
Service是安卓中系统的组件,它运行在独立进程的主线程中,不可以执行耗时操作。
Thread是程序执行的最小单元,分配CPU的基本单位,可以开启子线程执行耗时操作。
Service在不同Activity中可以获取自身实例,可以方便的对Service进行操作。
Thread在不同的Activity中难以获取自身实例,如果Activity被销毁,Thread实例就很难再获取得到。
五、使用IntentService
IntentService是 Scrvice 的子类,因此它不是普通的Service,它比普通的Service增加了额外的功能。
先看Service本身存在的两个问题。
-
Service不会专门启动一个单独的进程,Service与它所在应用位于同一个进程中。
-
Service不是一条新的线程,因此不应该在Service中直接处理耗时的任务。
IntentService正好弥补了Service的不足。
IntentService的特点:
总结
找工作是个很辛苦的事情,而且一般周期都比较长,有时候既看个人技术,也看运气。第一次找工作,最后的结果虽然不尽如人意,不过收获远比offer大。接下来就是针对自己的不足,好好努力了。
最后为了节约大家的时间,我把我学习所用的资料和面试遇到的问题和答案都整理成了PDF文档
喜欢文章的话请关注、点赞、转发 谢谢!
参考docs.qq.com/doc/DSkNLaERkbnFoS0ZF
Service在不同Activity中可以获取自身实例,可以方便的对Service进行操作。
Thread在不同的Activity中难以获取自身实例,如果Activity被销毁,Thread实例就很难再获取得到。
五、使用IntentService
IntentService是 Scrvice 的子类,因此它不是普通的Service,它比普通的Service增加了额外的功能。
先看Service本身存在的两个问题。
-
Service不会专门启动一个单独的进程,Service与它所在应用位于同一个进程中。
-
Service不是一条新的线程,因此不应该在Service中直接处理耗时的任务。
IntentService正好弥补了Service的不足。
IntentService的特点:
总结
找工作是个很辛苦的事情,而且一般周期都比较长,有时候既看个人技术,也看运气。第一次找工作,最后的结果虽然不尽如人意,不过收获远比offer大。接下来就是针对自己的不足,好好努力了。
最后为了节约大家的时间,我把我学习所用的资料和面试遇到的问题和答案都整理成了PDF文档
喜欢文章的话请关注、点赞、转发 谢谢!
[外链图片转存中…(img-NDOzuTZY-1724351169282)]
参考docs.qq.com/doc/DSkNLaERkbnFoS0ZF