保护service不被杀死(有些机型不可实现,待更新)

保护service不被杀死(有些机型不可实现,待更新)

详细代码在此贴出:https://github.com/yzbbanban/Test_Romote
此案例支持以下型号手机(感谢腾讯优测平台):
Nokia 6 7.0可以
华为mate 9 7.0失败
华为5.1 可以
三星 s6 可以
oppo R7 plus 可以
小米死掉
魅族死掉
锤子死掉
索尼Z5 6.0 C5成功
金立 可以
htc 5.0,6.0可以
酷派 可以
moto Z 6.0失败
moto x style 5.1可以
努比亚 5.1可以
LG 6.0可以
乐视死掉
vivo 6.0可以
以上是测试的全部部分,以后待更新了


用的是一个大神的框架,写的也很详细,详细的地址:http://blog.csdn.net/marswin89/article/details/50917098

下面介绍下用法,在上面github上下载文件后,其中有一个重要的文件module:LibMarsdaemon,就是这个会有用,打开android studio,添加加入:module
这里写图片描述

这里写图片描述
可以看到已经存在项目中
这里写图片描述
下面看下Project structure的内容:
这里写图片描述
下面添加到依赖中,作为libs
这里写图片描述
加到app的库中,然后一路OK即可
这里写图片描述
看到app中有lib即可


下面开始代码部分:很简单:
首先在manifest.xml中注册需要守护的service,以及其他唤起的service、receiver:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.ban.com.test_remote">

    <application
        android:name=".MyApplication2"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--需要守护的service-->
        <service android:name=".Service1" android:process=":process1"/>
        <receiver android:name=".Receiver1" android:process=":process1"/>
        <service android:name=".Service2" android:process=":process2"/>
        <receiver android:name=".Receiver2" android:process=":process2"/>
    </application>

</manifest>

然后分别写出几个Service1、Service2与Receiver1、Receiver2

package test.ban.com.test_romote;

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

/**
 * This Service is Persistent Service. Do some what you want to do here
 *
 * Created by brander on 2017/3/21.
 */
public class Service1 extends Service {
   @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onStartCommand: ");
    }

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

    private static final String TAG = "Service1";
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");

        return super.onStartCommand(intent, flags, startId);
    }
}
package com.marswin89.marsdaemon.demo;

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

/**
 * DO NOT do anything in this Service!
 *
 * Created by brander on 2017/3/21.
 */
public class Service2 extends Service{

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_NOT_STICKY;
    }
}
package com.marswin89.marsdaemon.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * DO NOT do anything in this Receiver!
 *
 * Created by brander on 2017/3/21.
 */
public class Receiver1 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}
package com.marswin89.marsdaemon.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * DO NOT do anything in this Receiver!
 *
 * Created by brander on 2017/3/21.
 */
public class Receiver2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}

完成四个组件,接下来在mainfest中注册自定义的application:

 android:name=".MyApplication"

一下就是MyApplication的详细代码(可直接复制):

package test.ban.com.test_markdown;

import android.app.Application;
import android.content.Context;

import com.marswin89.marsdaemon.DaemonClient;
import com.marswin89.marsdaemon.DaemonConfigurations;

/**
 * Created by brander on 2017/3/21.
 */
public class MyApplication extends Application {
    //进程守护
    private DaemonClient mDaemonClient;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        mDaemonClient = new DaemonClient(createDaemonConfigurations());
        mDaemonClient.onAttachBaseContext(base);
    }

    private DaemonConfigurations createDaemonConfigurations() {
        DaemonConfigurations.DaemonConfiguration configuration1 = new DaemonConfigurations.DaemonConfiguration(
                "test.ban.com.test_markdown:process1",//直接写包名+进程
                Service1.class.getCanonicalName(),
                Receiver1.class.getCanonicalName());
        DaemonConfigurations.DaemonConfiguration configuration2 = new DaemonConfigurations.DaemonConfiguration(
                "test.ban.com.test_markdown:process2",//直接写包名+进程
                Service2.class.getCanonicalName(),
                Receiver2.class.getCanonicalName());
        DaemonConfigurations.DaemonListener listener = new MyDaemonListener();
        //return new DaemonConfigurations(configuration1, configuration2);//listener can be null
        return new DaemonConfigurations(configuration1, configuration2, listener);
    }


    class MyDaemonListener implements DaemonConfigurations.DaemonListener {
        @Override
        public void onPersistentStart(Context context) {
        }

        @Override
        public void onDaemonAssistantStart(Context context) {
        }

        @Override
        public void onWatchDaemonDaed() {
        }
    }
}

下面是在MainActivity中启动service:

package test.ban.com.test_markdown;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(MainActivity.this,Service1.class));
    }
}

现在已经完成进程守护部分,现在可以在Service1中写入你想要的效果:
比如定时发送数据:

package test.ban.com.test_markdown;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * This Service is Persistent Service. Do some what you want to do here.<br/>
 * <p>
 * Created by brander on 2017/3/21.
 */
public class Service1 extends Service {
    private Timer mTimer;
    private TimerTask mTimerTask;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(Service1.this, "brander", Toast.LENGTH_SHORT).show();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        //TODO do some thing what you want..
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(1);
            }
        };
        mTimer.schedule(mTimerTask, 1000L, 3000L);//延迟1s后,每隔三秒执行
    }

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

这样可以了,尝试杀死他吧(注:有时候6.0在息屏状态,且不插电源的情况下,会出现被杀掉,原因待分析中)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值