应用双进程白色保活

清单配置:

<service
            android:name="com.service.Service1"
            android:enabled="true"
            android:process=":Service1">
        </service>
        <service
            android:name="com.service.Service2"
            android:enabled="true"
            android:process=":Service2">
        </service>

代码片段:

public class Service1 extends Service {
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentInfo("补充内容");
        builder.setContentText("正在运行...");
        builder.setContentTitle("进行标题");
        builder.setSmallIcon(R.mipmap.ic_laucher);
        builder.setTicker("新消息");
        builder.setAutoCancel(true);
        builder.setWhen(System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, SplashActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        builder.setContentIntent(pendingIntent);
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            notification = builder.build();
        } else {
            notification = builder.getNotification();
        }
        //把该service创建为前台service
        startForeground(startId, notification);

        thread.start();
        return START_STICKY;
    }

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    boolean b = ServiceUtils.isServiceWork(Service1.this, "com.service.Service2");
                    if(!b) {
                        Intent service = new Intent(Service1.this, Service2.class);
                        startService(service);
                    }
                }
            };
            timer.schedule(task, 0, 1000);
        }
    });

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}
public class Service2 extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        thread.start();
        return START_REDELIVER_INTENT;
    }

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    boolean b = ServiceUtils.isServiceWork(MyJobService.this, "com.service.Service1");
                    if(!b) {
                        Intent service = new Intent(Service2.this, Service1.class);
                        startService(service);
                    }
                }
            };
            timer.schedule(task, 0, 1000);
        }
    });

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

package com.zigsun.util;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;

/**
 * Created by Seal on 2017/8/14.
 */

public class ServiceUtils {
    /**
     * 判断某个服务是否正在运行的方法
     *
     * @param mContext
     * @param serviceName
     *            是包名+服务的类名
     * @return
     */
    public static boolean isServiceWork(Context mContext, String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(100);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        return isWork;
    }

    /**
     * 判断进程是否运行
     *
     * @return
     */
    public static boolean isProessRunning(Context context, String proessName) {

        boolean isRunning = false;
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        List<ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : lists) {
            if (info.processName.equals(proessName)) {
                isRunning = true;
            }
        }

        return isRunning;
    }
}

在Activity中启用Service:

    private void initService() {
        boolean isRun2 = ServiceUtils.isServiceWork(SplashActivity.this,
                "com.service.Service1");
        if (isRun2 == false) {
            startService(new Intent(SplashActivity.this, Service1.class));
            try {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        boolean isRun1 = ServiceUtils.isServiceWork(SplashActivity.this,
                "com.service.Service2");
        if (isRun1 == false) {
            startService(new Intent(SplashActivity.this, Service2.class));
            try {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值