zoukankan      html  css  js  c++  java
  • Android 创建永不Kill的Service

     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.xml的application标签中添加android:persistent属性

    <application
            android:icon="@drawable/app_default"
            android:label="@string/app_name"
            android:persistent="true" >
            ........................................
    </application>
    

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

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

    public void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    		
    	Intent localIntent = new Intent();
            localIntent.setClass(this, TestService.class);  //销毁时重新启动Service
            this.startService(localIntent);
    }
    

      

     

  • 相关阅读:
    Java 中几种常用的线程池
    阿里巴巴java工程师面试经验详情
    设计模式-备忘录模式
    设计模式-职责链模式
    设计模式-中介者模式
    设计模式-解释器模式
    设计模式-观察者模式
    设计模式-迭代器模式
    设计模式-命令模式
    设计模式-模板方法模式
  • 原文地址:https://www.cnblogs.com/DswCnblog/p/2655819.html
Copyright © 2011-2022 走看看