建立前台 Service

 Service在后台运行时,若是系统内存内存不足时,就有可能会回收掉后台运行的Service,ui

 若是想要Service一直运行,而不会由于内存不足时被回收,就能够使用前台Service;this

 前台Service和普通Service的区别:spa

 

 前台Service会有个图标显示在系统的状态栏,下拉状态栏会有更详细的信息对象

 前台Service的建立blog

  在onCreate()方法中:内存

  一、建立一个Notification对象。并设置参数;get

  二、调用startForeground(1,notification); //启动为前台Serviceit

 

public void onCreate()
	{				
		super.onCreate();
		
		Intent intent = new Intent(this,MainActivity.class);
		PendingIntent pend = PendingIntent.getActivity(this,0, intent,0);
		 
		//建立Notification
		NotificationCompat.Builder nc = new Builder(this);
			nc.setContentTitle("前台Service");
			nc.setContentText("有新消息");
			nc.setSmallIcon(R.drawable.ic_launcher);
			nc.setContentIntent(pend);
			Notification notifivatiion = nc.build();
			
			//开启模式为     前台Service
			startForeground(1,notifivatiion);	
	}
相关文章
相关标签/搜索