首页 > 其他 > 详细

防止服务被杀死的方法

时间:2014-02-28 05:20:52      阅读:492      评论:0      收藏:0      [点我收藏+]

 

 

//注册Intent.ACTION_TIME_TICK过滤器的接收器,每分钟接收一次
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
NoKillReceiver receiver = new NoKillReceiver();
registerReceiver(receiver, filter);
MainActivity.startActivity=this;

//接收器代码,接收一次就判断需要打开的服务是否还在运行,如果已经被关闭折再次启动

bubuko.com,布布扣
package com.caicai.receiver;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.caicai.service.PushService;

public class NoKillReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) { 
            boolean isServiceRunning = false; 
            ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); 
            for (RunningServiceInfo service :manager.getRunningServices(Integer.MAX_VALUE)) { 
            if("com.caicai.service.caiPushService".equals(service.service.getClassName())) { 
            isServiceRunning = true; 
            }   
             } 
            Intent i = new Intent(context, PushService.class); 
            context.startService(i); 
            if (!isServiceRunning) { 
    
            }else{
                 Log.i("tt", "166666666666666666666666");
            } 
            } 
    }

}
bubuko.com,布布扣

//需要启动的服务

bubuko.com,布布扣
package com.caicai.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class PushService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
     @Override
        public void onStart(Intent intent, int startId) {
         Log.i("tt", "15555555555555555555555555555555555555555555555555555");
        }
}
bubuko.com,布布扣

防止服务被杀死的方法,布布扣,bubuko.com

防止服务被杀死的方法

原文:http://www.cnblogs.com/clarence/p/3571072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!