使用startForeground()创建前台Service
来源:互联网 发布:vb乘法表 编辑:程序博客网 时间:2024/06/05 11:09
Service默认是运行在后台的,意味着系统如果需要回收内存的话,就可能会杀掉后台的服务,如果某个Service被杀掉对用户具有很大影响的话,可以把Service设置成前台的,这样的Service优先级会比较高,被杀掉的概率也会很低。
比如正在后台播放音乐的Service,如果被杀掉,音乐会停止播放,这样用户就会注意到。所以可以看到许多音乐播放器在后台运行的时候,会在下拉的通知栏上有正在运行的通知显示给用户,这样可以保证Service不会被轻易杀掉。
通过调用Service的startForeground(int id, Notification notification)设置前台服务,第一个参数是一个不为0的正整数,代表通知的id,第二个参数代表显示在通知栏的提示。
一个前台Service的实例
ForegroundService.java
public class ForegroundService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Intent activityIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(getApplication(), 0, activityIntent, 0); Notification notification = new Notification.Builder(getApplication()).setAutoCancel(true). setSmallIcon(R.drawable.ic_launcher).setTicker("前台Service启动").setContentTitle("前台Service运行中"). setContentText("这是一个正在运行的前台Service").setWhen(System.currentTimeMillis()).setContentIntent(pendingIntent).build(); startForeground(1, notification); return START_STICKY; }}
使用PendingIntent构造了一个Notification,点击通知栏的通知消息后,会启动PendingIntent所指定的Activity,即MainActivity
MainActivity.java
public class MainActivity extends ActionBarActivity { Button startService; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService = (Button)findViewById(R.id.start); startService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub intent = new Intent(MainActivity.this, ForegroundService.class); startService(intent); } }); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); stopService(intent); }}
MainActivity中的逻辑很简单,只有一个Button,点击Button后启动Service,在Activity结束时停止Service
运行程序,点击Button启动Service
如果想要把前台Service再设置为后台Service,调用stopForeground()方法,传入一个布尔型参数,表示是否删除状态栏通知,此方法不会终止服务,只是把前台Service设置为后台Service。
修改ForegroundService中的代码
@Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Intent activityIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0); Notification notification = new Notification.Builder(this).setAutoCancel(true). setSmallIcon(R.drawable.ic_launcher).setTicker("前台Service启动").setContentTitle("前台Service运行中"). setContentText("这是一个正在运行的前台Service").setWhen(System.currentTimeMillis()). setContentIntent(pendingIntent).build(); startForeground(1, notification); new Timer().schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub stopForeground(true); } }, 3000); return START_STICKY; }
在onStartCommand中启动了一个定时器,3秒后调用stopForeground(true)将Service设置为后台Service,通知栏的通知就会消失,运行程序
另外,如果服务被终止,状态栏的通知也会消失,在上面的程序中,在Activity的onStop()中停止了服务,运行程序,启动Service后退出Activity
如果应用程序需要在API level<5的平台上运行,可以使用如下的类来调用旧的setForeground()方法:
development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java
- 使用startForeground()创建前台Service
- android服务- Notification创建通知 - startForeground 使用前台服务
- 关于Service使用startForeground()方法
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- 使用startForeground让android服务前台运行
- Android的startForeground前台Service如何去掉通知显示
- 【Service】创建前台Service
- 前台运行服务startForeground
- 创建前台Service
- android 服务前台运行startForeground
- 安卓开发40:使用startForeground让android服务前台运行
- 如何创建前台Android Service
- MySQL学习笔记---基础概念和一些基础SQL语句(二)
- git 使用详解
- IDCT变换(JAVA)
- 学习笔记(一)防火墙认识
- “师创杯”山东理工大学第九届ACM程序设计竞赛 正式赛 I.皮卡丘的梦想2【树状数组】水题
- 使用startForeground()创建前台Service
- 一次git事故的回顾和学习
- django学习--models
- TransactionDefinition接口中定义了七个事务传播行为
- Chinese Poetry Generation with Recurrent Neural Networks
- 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping
- Python 3 str()与repr()、__str__与__repr__:
- 关于c++类的空间分配以及存储
- Java基础零碎练手<二>