前台服务是那些被认为用户知道且在系统内存不足的时候不容许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下——这就意味着通知只有在这个服务被终止或从前台主动移除通知后才能被解除。java
最多见的表现形式就是音乐播放服务,应用程序后台运行时,用户能够经过通知栏,知道当前播放内容,并进行暂停、继续、切歌等相关操做。android
后台运行的Service系统优先级相对较低,当系统内存不足时,在后台运行的Service就有可能被回收,为了保持后台服务的正常运行及相关操做,能够选择将须要保持运行的Service设置为前台服务,从而使APP长时间处于后台或者关闭(进程未被清理)时,服务可以保持工做。git
public class ForegroundService extends Service { private static final String TAG = ForegroundService.class.getSimpleName(); @Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate"); } @Nullable @Override public IBinder onBind(Intent intent) { Log.e(TAG, "onBind"); return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG, "onDestroy"); super.onDestroy(); } }
/** * 建立服务通知 */ private Notification createForegroundNotification() { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 惟一的通知通道的id. String notificationChannelId = "notification_channel_id_01"; // Android8.0以上的系统,新建消息通道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //用户可见的通道名称 String channelName = "Foreground Service Notification"; //通道的重要程度 int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance); notificationChannel.setDescription("Channel description"); //LED灯 notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); //震动 notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); notificationChannel.enableVibration(true); if (notificationManager != null) { notificationManager.createNotificationChannel(notificationChannel); } } NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId); //通知小图标 builder.setSmallIcon(R.drawable.ic_launcher); //通知标题 builder.setContentTitle("ContentTitle"); //通知内容 builder.setContentText("ContentText"); //设定通知显示的时间 builder.setWhen(System.currentTimeMillis()); //设定启动的内容 Intent activityIntent = new Intent(this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); //建立通知并返回 return builder.build(); }
@Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate"); // 获取服务通知 Notification notification = createForegroundNotification(); //将服务置于启动状态 ,NOTIFICATION_ID指的是建立的通知的ID startForeground(NOTIFICATION_ID, notification); }
@Override public void onDestroy() { Log.e(TAG, "onDestroy"); // 标记服务关闭 ForegroundService.serviceIsLive = false; // 移除通知 stopForeground(true); super.onDestroy(); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand"); // 标记服务启动 ForegroundService.serviceIsLive = true; // 数据获取 String data = intent.getStringExtra("Foreground"); Toast.makeText(this, data, Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent, flags, startId); }
以上就是前台服务的建立过程,相关注释已经很明白了,具体使用能够查看文末的Demo。github
服务建立完毕,接下来就能够进行服务的启动了,启动前不要忘记在清单文件中进行前台服务权限的添加:ide
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
//启动服务 if (!ForegroundService.serviceIsLive) { // Android 8.0使用startForegroundService在前台启动新服务 mForegroundService = new Intent(this, ForegroundService.class); mForegroundService.putExtra("Foreground", "This is a foreground service."); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(mForegroundService); } else { startService(mForegroundService); } } else { Toast.makeText(this, "前台服务正在运行中...", Toast.LENGTH_SHORT).show(); }
//中止服务 mForegroundService = new Intent(this, ForegroundService.class); stopService(mForegroundService);
关于前台服务的介绍及使用就到这里了,相关使用已上传至Github开发记录,欢迎点击查阅及Star,我也会继续补充其它有用的知识及例子在项目上。工具
欢迎关注公众号:几圈年轮,查看更多有趣的技术、工具、闲言、资源。
ui