当前位置: 移动技术网 > 移动技术>移动开发>Android > Android通知栏前台服务的实现

Android通知栏前台服务的实现

2020年05月11日  | 移动技术网移动技术  | 我要评论

一、前台服务的简单介绍

前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在运行(ongoing)标题之下——这就意味着通知只有在这个服务被终止或从前台主动移除通知后才能被解除。

最常见的表现形式就是音乐播放服务,应用程序后台运行时,用户可以通过通知栏,知道当前播放内容,并进行暂停、继续、切歌等相关操作。

二、为什么使用前台服务

后台运行的service系统优先级相对较低,当系统内存不足时,在后台运行的service就有可能被回收,为了保持后台服务的正常运行及相关操作,可以选择将需要保持运行的service设置为前台服务,从而使app长时间处于后台或者关闭(进程未被清理)时,服务能够保持工作。

三、前台服务的详细使用

创建服务内容,如下(四大组件不要忘记清单文件进行注册,否则启动会找不到服务);

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。

服务创建完毕,接下来就可以进行服务的启动了,启动前不要忘记在清单文件中进行前台服务权限的添加:

<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,我也会继续补充其它有用的知识及例子在项目上。

到此这篇关于android通知栏前台服务的实现的文章就介绍到这了,更多相关android 通知栏前台内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网