永不Kill的Service

本文介绍了如何在Android应用中调整服务优先级、配置持久化属性以确保应用在多任务环境下保持运行,并通过在Service的onDestroy()方法中重启服务实现更稳定的应用体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、提升服务的优先级

 

Android AndroidManifest.xml 里面给服务增加优先级,通过content.StartService();方式启动服务。1000是最高值,如果数字越小则优先级越低

 


<service android:name=".push.PushService" >

    <intent-filter android:priority="1000">

          <action android:name="com.xsl.push" />

    </intent-filter>

</service>


Intent intent = new Intent();

intent .setAction("com.xsl.push");

context.startService(intent );

 

 

2、在Android AndroidManifest.xmlapplication标签中添加android:persistent属性

 


<application

        android:icon="@drawable/app_default"

        android:label="@string/app_name"

        android:persistent="true" >

        ........................................

</application>

  切记,这个不可滥用,系统中用这个的serviceapp一多,整个系统就完蛋了

 

3、在ServiceonDestroy()中重启Service.这种方式,用户在无法再设置-运行的服务中将此服务停止

 


public void onDestroy() {

    // TODO Auto-generated method stub

    super.onDestroy();

        

    Intent localIntent = new Intent();

        localIntent.setClass(this, TestService.class);  //销毁时重新启动Service

        this.startService(localIntent);

}


转自http://blog.sina.com.cn/s/blog_76c545390101gl63.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值