优惠券网站是不是很难做,如何做好购物网站,花卉公司网页设计,wordpress的视频封面能动目录
一#xff0c;Service简介 二#xff0c;Service的两种启动方式
1#xff0c;非绑定式启动Service
2#xff0c;绑定式启动Service
三#xff0c;Service的生命周期
1#xff0c;非绑定式Service的生命周期
2#xff0c;绑定式Service的生命周期
四#xf…目录
一Service简介 二Service的两种启动方式
1非绑定式启动Service
2绑定式启动Service
三Service的生命周期
1非绑定式Service的生命周期
2绑定式Service的生命周期
四前台Service
1前台Service的创建
2前台Service的结束 一Service简介 Service服务是指执行指定系统功能的程序例程或进程以便支持其他程序并且运行期间用户不可见的一种活动机制例如后台播放音乐后台下载等 Service和Activity同属于一个级别不同于子线程service是运行在主线程中的因此不能进行耗时操作 二Service的两种启动方式 1非绑定式启动startService 服务开启后与启动者没有任何关系service的生命周期独立于启动者启动者退出service仍会运行启动者无法调用service中的方法 2绑定式启动bindService 启动者Activity会和service绑定在一起两者的生命周期会同步当启动者退出时service会跟着被销毁启动者可以调用service中的方法 1非绑定式启动Service
1 创建一个类继承Service类并重写一系列方法
public class MyService extends Service {NullableOverridepublic IBinder onBind(Intent intent) {Log.i(MyService, onBind: );return null;}Overridepublic void onCreate() {Log.i(MyService, onCreate: );super.onCreate();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(MyService, onStartCommand: );return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.i(MyService, onDestroy: );super.onDestroy();}
}
2在Manifest文件中注册指定Service 3调用startServiceIntent intent方法启动Service
private void startMyService() {Intent intent new Intent(this, MyService.class);startService(intent);
}
2绑定式启动Service
1前两步与非绑定式启动一致创建Service子类并注册Service
public class MyBindService extends Service {private final String TAG MyBindService;NullableOverridepublic IBinder onBind(Intent intent) {Log.i(TAG, onBind: );return new MyBinder(this);}Overridepublic void onCreate() {Log.i(TAG, onCreate: );super.onCreate();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, onStartCommand: );return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.i(TAG, onDestroy: );super.onDestroy();}Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, onUnbind: );return super.onUnbind(intent);}
}
2绑定式启动Service需要调用bindService方法这个方法需要三个参数
private void startBindService() {Intent intent new Intent(this, MyBindService.class);isBound bindService(intent, connection, BIND_AUTO_CREATE);
} Intent表示启动意图也就是想要启动的Service connection相当于启动者Activity和Service之间的连接通过一系列的回调函数来监听访问者和Service的连接情况 int flag绑定时是否自动创建Service这里选择自动创建BIND_AUTO_CREATE 除了Intent和flag外我们还需创建一个connection这里通过匿名内部类的形式创建并重写两个回调方法。这里onServiceConnected方法中有一个IBinder类型的service这个service起到了中间人的作用通过这个service启动者Activity就可以调用Service中的方法
private ServiceConnection connection new ServiceConnection() {//创建连接时回调Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//这里 IBinder类型的service 就是我们要绑定的那个service//通过这个serviceActivity就可以调用MyBindService.MyBinder中的方法}//断开连接时回调Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, onServiceDisconnected: );}
}; 那么这个service是从哪来的呢 在我们创建的Service子类中我们重写了一个onBind的方法返回的正好是一个IBinder类型的值这个返回值也就是会传给上面service的值。
Override
public IBinder onBind(Intent intent) {Log.i(TAG, onBind: );return new MyBinder(this);
} 所以我们可以在Service子类中创建一个类继承自BinderBinder实现了IBinder接口这样Activity通过connection中的service就可以调用MyBinder类中的方法 进一步通过构造方法我们可以将Service传给MyBinder这样在MyBinder中就可以调用我们Service中的方法又因为Activity可以调用MyBinder中的方法所以我们就实现了Activity调用Service的方法这也就是为什么绑定式启动Service启动者Activity可以调用Service中的方法
Override
public IBinder onBind(Intent intent) {Log.i(TAG, onBind: );return new MyBinder(this);
}public void Test(){//Log.i(TAG, Test: MyBindService的Test方法被调用);
}public class MyBinder extends Binder{private MyBindService myBindService;public MyBinder(){}public MyBinder(MyBindService bindService){this.myBindService bindService;}public void Test(){//Log.i(TAG, Test: MyBinder的Test方法被调用);//这样MyBinder就可以调用MyBindService中的方法//MyBinder作为一个中间人 Activity调用MyBinder的方法 - MyBinder再调用Service的方法myBindService.Test();}
}
绑定式启动Service的全部流程代码
Activity
public class MainActivity extends AppCompatActivity {private final String TAG MainActivity;private Boolean isBound false;private ActivityMainBinding binding;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());setLinsteners();}private void setLinsteners() {binding.btnStartBindService.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {startBindService();}});}private void startBindService() {Intent intent new Intent(this, MyBindService.class);isBound bindService(intent, connection, BIND_AUTO_CREATE);}private MyBindService.MyBinder myBindService;private ServiceConnection connection new ServiceConnection() {//创建连接时回调Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//这里 IBinder类型的service 就是我们要绑定的那个service//通过这个serviceActivity就可以调用MyBindService.MyBinder中的方法myBindService (MyBindService.MyBinder)service;myBindService.Test();}//断开连接时回调Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, onServiceDisconnected: );//Intent intent new Intent(MainActivity.this, MyBindService.class);//stopService(intent);}};
}
Service
public class MyBindService extends Service {private final String TAG MyBindService;NullableOverridepublic IBinder onBind(Intent intent) {Log.i(TAG, onBind: );return new MyBinder(this);}public void Test(){//Log.i(TAG, Test: MyBindService的Test方法被调用);}public class MyBinder extends Binder{private MyBindService myBindService;public MyBinder(){}public MyBinder(MyBindService bindService){this.myBindService bindService;}public void Test(){//Log.i(TAG, Test: MyBinder的Test方法被调用);//这样MyBinder就可以调用MyBindService中的方法//MyBinder作为一个中间人 Activity调用MyBinder的方法 - MyBinder再调用Service的方法myBindService.Test();}}Overridepublic void onCreate() {Log.i(TAG, onCreate: );super.onCreate();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, onStartCommand: );return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.i(TAG, onDestroy: );super.onDestroy();}Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, onUnbind: );return super.onUnbind(intent);}}
三Service的生命周期
1非绑定式Service的生命周期 启动阶段启动者Activity调用startService onCreateService被创建时调用整个生命周期中只会被调用一次 onStartCommand每次调用startService时该方法会被调用该方法接收Intent参数从而可以执行一些命令 结束阶段启动者调用stopService方法或Service内部调用stopSelf方法 onDestroyService销毁时调用与onCreate一样整个生命周期中只会被调用一次 2绑定式Service的生命周期 启动阶段启动者Activity调用bindService onCreateService被创建时调用整个生命周期中只会被调用一次onBind在首次绑定时会被调用一次同样整个生命周期中只会被调用一次 结束阶段当启动者销毁或unBindService方法时启动者会和Service解除绑定当没有任何绑定者时Service会被销毁 onUnbind解除绑定时调用可多次调用onDestroyService销毁时调用整个生命周期中只会被调用一次 四前台Service 前台Service即可以与用户进行交互的运行在前台的Service优先级相比于其他两种运行在后台的Service要高最常见的应用就是通知栏前台控制音乐播放 1前台Service的创建 在正常的Service中调用startForeground 方法即可将正常服务提升为前台服务startForeground方法需要接收一个通知对象因为前台Service必须在通知栏中进行通知
public class MyForeGroundService extends Service {private final String TAG MyForeGroundService;NullableOverridepublic IBinder onBind(Intent intent) {Log.i(TAG, onBind: );return null;}Overridepublic void onCreate() {super.onCreate();Log.i(TAG, onCreate: );//创建一个通知NotificationManager notificationManager (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel new NotificationChannel(channel_id,channel_name,notificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);Notification.Builder builder new Notification.Builder(this,channel_id);Notification notification builder.build();//将服务提升为前台服务startForeground(1, notification);}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, onStartCommand: );return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.i(TAG, onDestroy: );super.onDestroy();}}
2前台Service的结束 前台Service的结束有两种含义 1结束Service本身通过启动者调用stopService方法或Service内部调用stopSelf方法正常结束ServiceService结束后通知也会随之移除 2前台Service降级为后台Service通过Service内部调用stopForegroundtrue方法将Service退出后台状态此时Service不会被销毁当内存不足时Service可能会被回收。参数true表示移除通知 前台Service创建和结束的全部流程代码
Activity
public class MainActivity extends AppCompatActivity {private final String TAG MainActivity;private Boolean isBound false;private ActivityMainBinding binding;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());setLinsteners();}private void setLinsteners() {//创建前台Servicebinding.btnStartForeGroundService.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {Intent intent new Intent(MainActivity.this, MyForeGroundService.class);startService(intent);}});//移除前台Servicebinding.btnStopForeGroundService.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {Intent intent new Intent(MainActivity.this, MyForeGroundService.class);intent.putExtra(key_stop, stopForeGround);startService(intent);}});}}
Service
public class MyForeGroundService extends Service {private final String TAG MyForeGroundService;NullableOverridepublic IBinder onBind(Intent intent) {Log.i(TAG, onBind: );return null;}Overridepublic void onCreate() {super.onCreate();Log.i(TAG, onCreate: );//创建一个通知NotificationManager notificationManager (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel new NotificationChannel(channel_id,channel_name,notificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);Notification.Builder builder new Notification.Builder(this,channel_id);Notification notification builder.build();//将服务提升为前台服务startForeground(1, notification);}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, onStartCommand: );String keyStop intent.getStringExtra(key_stop);if(TextUtils.equals(keyStop, stopForeGround)){stopForeground(true);//true表示移除通知}return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.i(TAG, onDestroy: );super.onDestroy();}}