Protect the service from being killed (some models are not available, to be updated)

Protect the service from being killed (some models are not available, to be updated)

The detailed code is posted here: https://github.com/yzbbanban/Test_Romote
This case supports the following models of mobile phones (thanks to Tencent Youtest Platform):
Nokia 6 7.0 is OK
Huawei mate 9 7.0 fails
Huawei 5.1 is OK
Samsung s6 is OK
oppo R7 plus Yes
Xiaomi is dead
Meizu is dead
Hammer is dead
Sony Z5 6.0 C5 is successful
Gionee is ok
htc 5.0, 6.0 is ok
Coolpad is
ok moto Z 6.0 is ok
moto x style 5.1 is ok
Nubia 5.1 is ok
LG 6.0 is ok
LeEco dead
vivo 6.0 is
ok All parts of , to be updated in the future


It uses a framework of a great god, and it is written in detail. The detailed address: http://blog.csdn.net/marswin89/article/details/50917098

The following introduces the usage. After downloading the file on github above, there is an important file module: LibMarsdaemon, which will be useful. Open android studio and add: module
Write picture description here

Write picture description here
You can see that
Write picture description here
there take a look at the contents of the Project structure:
Write picture description here
add it to the dependencies below,
Write picture description here
add it to the app’s library as libs, and then OK all the way. You can
Write picture description here
see that there is a lib in the app.


Let's start the code part: very simple:
first register the service that needs to be guarded in manifest.xml, and other services and receivers that are invoked:

<?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>

Then write several Service1, Service2 and Receiver1, Receiver2 respectively

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) {

    }
}

Complete the four components, and then register the custom application in the mainfest:

 android:name=".MyApplication"

The following is the detailed code of MyApplication (can be copied directly):

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() {
        }
    }
}

The following is to start the service in MainActivity:

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));
    }
}

Now that the process daemon part has been completed, you can now write the effect you want in Service1:
for example, send data regularly:

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;
    }
}

This is ok, try to kill him (Note: Sometimes 6.0 will be killed when the screen is off and the power is not plugged in, the reason is to be analyzed)

Supongo que te gusta

Origin blog.csdn.net/u013377003/article/details/65631240
Recomendado
Clasificación