使android service在进程清理时不被结束
来源:互联网 发布:网上发短信软件 编辑:程序博客网 时间:2024/06/05 16:28
Notification除了用于后台服务通知,还常用在下面情况:
(1)保持服务存在。当系统内存不足时,系统会认为某后台服务占用内存时间太长而中止该服务,以释放内存。对于某些服务,例如播放音乐,如果系统对该服务进行资源释放,用户体验就成了音乐突然没有声音。对这类服务,我们希望享有更高的优先级别,不会被系统干掉。
(2)用户随时与服务进行互动。例如播放音乐的服务,用户可随时暂停音乐播放,或选择其他曲目,甚至中止播放音乐服务。
要实现上述两点,方法是在Service中宣称自己是foreground,并维持一个通知来向用户表明foreground状态,用户可以通过下拉通知并点击通知,进入该服务界面,实施互动操作。
我们将在Android学习笔记(五二):服务Service(中)- 继承Service类的模拟音乐播放例子上进行扩展。
FakePlayer同原例子的客户端代码,略过不表。Service的代码如下
public class FakePlayerService extends Service{
public static final String EXTRA_PLAYLIST="EXTRA_PLAYLIST";
public static final String EXTRA_SHUFFLE="EXTRA_SHUFFLE";
private boolean isPlay = false;
private static final int NOTIFY_FAKEPLAYER_ID=1339;
public IBinder onBind(Intent arg0) {
return null;
}
public void onDestroy() {
stop();
}
public int onStartCommand(Intent intent, int flags, int startId) {
String playlist = intent.getStringExtra(EXTRA_PLAYLIST);
Boolean isShuffle = intent.getBooleanExtra(EXTRA_SHUFFLE, false);
play(playlist,isShuffle);
return START_NOT_STICKY;
}
private void play(String playlist, Boolean isShuffle){
if(!isPlay){
isPlay = true;
//和上一笔记中创建通知的步骤一样,只是不需要通过通知管理器进行触发,而是用startForeground(ID,notify)来处理
//步骤1:和上一笔记一样,通过Notification.Builder( )来创建通知
//FakePlayer就是两个大button的activity,也即服务的界面,见最左图
Intent i = new Intent(this,FakePlayer.class);
//注意Intent的flag设置:FLAG_ACTIVITY_CLEAR_TOP: 如果activity已在当前任务中运行,在它前端的activity都会被关闭,它就成了最前端的activity。FLAG_ACTIVITY_SINGLE_TOP: 如果activity已经在最前端运行,则不需要再加载。设置这两个flag,就是让一个且唯一的一个activity(服务界面)运行在最前端。
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Notification myNotify = new Notification.Builder(this)
.setSmallIcon(R.drawable.shield)
.setTicker("Fake Player: " + playlist)
.setContentTitle("Test")
.setContentText("Now Playing: \"Ummmm, Nothing\"")
.setContentIntent(pi)
.build();
//设置notification的flag,表明在点击通知后,通知并不会消失,也在最右图上仍在通知栏显示图标。这是确保在activity中退出后,状态栏仍有图标可提下拉、点击,再次进入activity。
myNotify.flags |= Notification.FLAG_NO_CLEAR;
// 步骤 2:startForeground( int, Notification)将服务设置为foreground状态,使系统知道该服务是用户关注,低内存情况下不会killed,并提供通知向用户表明处于foreground状态。
startForeground(NOTIFY_FAKEPLAYER_ID,myNotify);
}
}
private void stop(){
if(isPlay){
isPlay = false;
//将服务从forefround状态中移走,使得系统可以在低内存的情况下清除它。
stopForeground(true);
}
}
}
相关链接: 我的Android开发相关文章
- 使android service在进程清理时不被结束
- android service防清理
- Android中如何让Service不被一键清理且在息屏后保活
- Android应用程序在新进程启动Activity及结束进程
- 清理android后台进程
- android 清理进程数据
- android实现结束进程。
- android 结束进程不起作用
- Android 结束/启动进程
- Android结束进程
- Android结束进程
- Android 结束进程
- 强制结束Android进程
- Android 双进程Service常驻后台,无惧“一键清理
- Android 双进程Service常驻后台,无惧“一键清理”
- Android 双进程Service常驻后台,无惧“一键清理”
- Android 双进程Service常驻后台,无惧“一键清理”
- android清理进程 shell脚本
- Windows Mobile C#实现GIF图片播放
- [翻译][Trident] Storm Trident 教程
- sqlite第三方类库:FMDB使用(转载)
- Android天气预报各种天气API
- QQ漫游消息、上传消息使用方法略记--希望能帮到有需要的人:测试版本为QQ2013(小版本没看)
- 使android service在进程清理时不被结束
- 用C#读写文本文件
- NG的Machine Learning终于上完了
- webmethods7 配置使用LDAP
- 使用maven打包及发布源码
- Xshell的常见问题
- (转+原创)c语言那些细节之a+1和&a+1的区别 ,指针的步长问题。
- 结构简介:input子系统
- ASP.NET 应用程序级别的事件处理程序