什么是Service
- “Service” 意思即“服务”的意思, 像 Windows 上面的服务一样,服务是在后台上运行,承担着静悄悄的不为人所注意的工作。Service运行在后台,它是不可见的、无界面的程序。
- Service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity,这个时候程序要在后台继续播放;比如检测SD卡上文件的变化;再或者在后台记录用户的地理信息位置的改变;或者启动一个服务来运行并一直监听某种动作等等。
- Service是四大组建之一,与Activity一样需要在清单文件中注册!
Service 特性
一、 Service在后台运行,不可以与用户直接交互;
二、 长时间运行,不占程序控制权;
三、 需要通过某一个Activity或其他Context对象来启动Service。context.startService() 或 context.bindService();
四、 Service很大程度上充当了应用程序后台线程管理器的角色。(如果Activity中新开启一个线程,当该Acitivity关闭后,该线程依然在工作,但是与开启它的Activity失去联系。也就是说此时的这个线程处于失去管理的状态。但是使用Service,则可以对后台运行的线程有效地管理。
Service启动方式
一、Context.startService()
调用者与服务之间没有关联,即使调用者退出,服务仍可运行被启动的服务是由其它组件调用startService()方法而启动的,该方法会导致被启动服务的生命周期方法onStartCommand()被回调。当服务是被启动状态后,其生命周期与启动它的组件无关,即使启动服务的组件(Activity,BroadcastReceiver)已经被销毁,该服务还可以在后台无限期运行。除非调用stopSelf()或stopService()来停止该服务
二、 Context.bindService()
调用者与服务绑定在一起,调用者一旦退出,服务也就终止绑定服务是允许其它应用程序绑定并且与之交互的Service的实现类。为了提供绑定,必须实现onBind()回调方法。该方法返回IBinder对象,它定义了服务类与Activity交互的程序接口。
Activity通过bindService()方法绑定到服务类,同时Activity必须提供ServiceConnection接口的实现类,它监视Activity与服务类之间的连接。在重写ServiceConnection接口的onServiceConnected()方法时,实现了将服务类顺利赋值到了Activity中,实现了在Activity中使用该服务类并执行其中的方法。
实现Service(start)
import android.app.Service;
一、public class MyService extends Service {
//重写里边的生命周期方法
onCreate onStart onDestroy
}
二、清单列表的application里配置
<service android:name=".MyService"
android:enabled="true">
<intent-filter>
<action android:name="MYSERVICE"></action>
</intent-filter>
</service>
三、
Intent intent = new Intent();
intent.setAction("MYSERVICE"); //添加action
intent.setPackage("com.ugrow.day02"); // 添加包名
startService(intent); //启动service
四、
stopService(intent); //停止service
实现Service(bind)
一、public class MyService extends Service {
public IBinder onBind(Intent intent) {
Log.d("TAG","onBind");
return new MyBinder();
}
public class MyBinder extends Binder{
public MyService getService(){
return MyService.this;
}
}
//重写onDestory
}
二、清单列表的application里配置
<service android:name=".MyService"
android:enabled="true">
<intent-filter>
<action android:name="MYSERVICE"></action>
</intent-filter>
</service>
三、 Intent intent1 = new Intent();
intent1.setAction("MYSERVICE");
intent1.setPackage("com.ugrow.day02"); //getPackageName()
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("TAG","service链接成功");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("TAG","service断开连接");
}
};
bindService(intent1,serviceConnection, Service.BIND_AUTO_CREATE);
四、 unbindService(serviceConnection);
Android系统服务
Android提供大量的系统服务,这些系统服务用于完成不同的功能
获取屏幕宽高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display defaultDisplay = wm.getDefaultDisplay();
int height = defaultDisplay.getHeight();
int width = defaultDisplay.getWidth();
Log.d("TAG","width==="+width);
Log.d("TAG","height==="+height);
NotificationManager
public void showWeather(){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(NotificationActivity.this);
builder.setSmallIcon(R.mipmap.sun);//设置图标
builder.setContentTitle("标题");
builder.setContentText("这是内容");
builder.setWhen(System.currentTimeMillis());//设置通知时间,默认为系统发出通知的时间,通常不用设置
builder.setAutoCancel(true);//打开程序后图标消失
Notification notification = builder.build();
nm.notify(1,notification);
}
Android 8.0 (API26)后新增了新增的通知渠道
builder.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//第一个参数 channelId
//第二个参数 channel名字
//第三个参数
NotificationChannel channel = new NotificationChannel
("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
builder.setChannelId("001");
}
点击通知后,没有反应。
//点击消息后的进行跳转
Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 1, intent, 0);
builder.setContentIntent(pendingIntent);
NotificationManager 清除消息
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建的时候 第一个参数 id
nm.notify(100,notification);
//清除的时候 根据创建的id进行清除
nm.cancel(100);
//清除的时候 根据创建的id进行清除
nm.cancelAll();
Service小结
- Service提供程序的后台服务,分为本地服务和远程服务两种类型
- 定义一个Service类需要只要继承Service类,并实现其生命周期中的方法
- Service必须在AndroidManifest.xml配置文件中通过元素进行声明
- Service有启动方式和绑定方式两种启动方式。
- Service的启动方式使用Context.startService()方法来启动一个Service,调用者与- - - Service之间没有关联,即使调用者退出,Service服务依然运行
- Service的绑定方式通过Context.bindService()来启动一个Service,调用者与Service之间绑定在一起,调用者一旦退出,Service服务也就终止使用Context.startService()方法启动的Service,通过调用Context.stopService()或Service.stopSelf()方法结束服务
- 使用Context.bindService()绑定的Service,通过调用Context.unbindservice()解除绑定的服务
- Android提供大量的系统服务,这些系统服务用于完成不同的功能,通过Context.getSystemService()方法可以获取不同服务管理对象
- NotificationManager类是系统的通知服务管理类,它能够将通知Notification信息显示在状态栏上