关于进程守护,拉起问题

之前看了一篇文章说的是进程的守护问题,忘记实在哪里看了,现在有时间吧代码发出来,做下笔记
这一块的问题 常见于推送相关的,因为有的时候系统会把,开启的服务给kill 了,但是服务不会自动启动
1.aidl 文件

// IProcessService.aidl
package com.stevefat.myapplication;

// Declare any non-default types here with import statements

interface IProcessService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
   String getServiceName();
}

2.第一个服务

package com.stevefat.myapplication.services;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

import com.stevefat.myapplication.IProcessService;

/**
 * Author: ngh
 * date: 2016/10/9
 */

public class DaemonService extends Service {
    String TAG = "DaemonService";

    private DaemonBinder mDaemonBinder;

    private DaemonServiceConnection mDaemonServiceConn;

    public DaemonService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mDaemonBinder = new DaemonBinder();

        if (mDaemonServiceConn == null) {
            mDaemonServiceConn = new DaemonServiceConnection();
        }

        Log.e(TAG, TAG + " onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.e(TAG, TAG + " onStartCommand");
        bindService(new Intent(this, RemoteService.class), mDaemonServiceConn, Context.BIND_IMPORTANT);

        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mDaemonBinder;
    }

    /**
     * 通过AIDL实现进程间通信
     */
    class DaemonBinder extends IProcessService.Stub {
        @Override
        public String getServiceName() throws RemoteException {
            return TAG;
        }
    }

    /**
     * 连接远程服务
     */
    class DaemonServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                // 与远程服务通信
                IProcessService process = IProcessService.Stub.asInterface(service);
                Log.e(TAG, "连接" + process.getServiceName() + "服务成功");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // RemoteException连接过程出现的异常,才会回调,unbind不会回调
            startService(new Intent(DaemonService.this, RemoteService.class));
            bindService(new Intent(DaemonService.this, RemoteService.class), mDaemonServiceConn, Context.BIND_IMPORTANT);
        }
    }
}

3.第二个服务

package com.stevefat.myapplication.services;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

import com.stevefat.myapplication.IProcessService;

/**
 * Author: ngh
 * date: 2016/10/9
 */

public class RemoteService extends Service {
    String TAG = "RemoteService";
    private ServiceBinder mServiceBinder;

    private RemoteServiceConnection mRemoteServiceConn;

    @Override
    public void onCreate() {
        super.onCreate();

        mServiceBinder = new ServiceBinder();

        if (mRemoteServiceConn == null) {
            mRemoteServiceConn = new RemoteServiceConnection();
        }

        Log.e(TAG, TAG + " onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.e(TAG, TAG + " onStartCommand");

        bindService(new Intent(this, DaemonService.class), mRemoteServiceConn, Context.BIND_IMPORTANT);

        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mServiceBinder;

}
    /**
     * 通过AIDL实现进程间通信
     */
    class ServiceBinder extends IProcessService.Stub {
        @Override
        public String getServiceName() throws RemoteException {
            return "RemoteService";
        }
    }


    /**
     * 连接远程服务
     */
    class RemoteServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {

                IProcessService process = IProcessService.Stub.asInterface(service);
                Log.e(TAG, "连接" + process.getServiceName() + "服务成功");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // RemoteException连接过程出现的异常,才会回调,unbind不会回调

            startService(new Intent(RemoteService.this, DaemonService.class));

            bindService(new Intent(RemoteService.this, DaemonService.class), mRemoteServiceConn, Context.BIND_IMPORTANT);
        }
    }
}

4.在配置清单文件中添加两个服务

<service android:name=".services.DaemonService"/>
<service android:name=".services.RemoteService"/>

5.在activity 中启动这两个服务

 Intent intent = new Intent(MainActivity.this,DaemonService.class);
 startService(intent);

 Intent intent1 = new Intent(MainActivity.this,RemoteService.class);
 startService(intent1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值