Android中的前置服务

来源:互联网 发布:excel如何去重复数据 编辑:程序博客网 时间:2024/05/20 22:39
在服务中有这个方法,可以绑定一个Notification,启动一个前置服务,就像金山软件那样,
void android.app.Service.startForeground(int id,Notification notification)

private void setUpAsForeground(String text,Class cls) {

PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, new Intent(getApplicationContext(),
cls), PendingIntent.FLAG_UPDATE_CURRENT);
mNotification = new Notification();
mNotification.tickerText = text;
mNotification.icon = R.drawable.logo;// TODO 更换服务前置的图标
mNotification.flags = Notification.FLAG_ONGOING_EVENT;
mNotification.icon = R.drawable.logo;
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotification.setLatestEventInfo(getApplicationContext(), "", text,
pendingIntent);
startForeground(NOTIFICATION_ID, mNotification);
}


stopForeground(true);//停止一个前置服务
      在项目中关于消息的前置服务,遇到了一个问题!当我在任何一个页面按home键时,都要启动前置服务,点击进入页面之后,就需要停止前置服务。要实现qq中的消息效果,可我们的项目中有多个页面,无法实现qq中的效果,最后就搞了一个父类,让用到的这多个activity都继承自父类,然后在父类的生命周期的方法中,对前置服务的的启动与停止做了处理,让每个子类在执行完自己的生命周期方法后,在执行以下父类的,这样,这个问题就解决了。




原创粉丝点击