AssistantService 启动成功
This commit is contained in:
		@@ -1,8 +1,8 @@
 | 
			
		||||
#Created by .winboll/winboll_app_build.gradle
 | 
			
		||||
#Thu Feb 13 23:29:49 GMT 2025
 | 
			
		||||
#Fri Feb 14 13:15:46 GMT 2025
 | 
			
		||||
stageCount=2
 | 
			
		||||
libraryProject=libappbase
 | 
			
		||||
baseVersion=1.5
 | 
			
		||||
publishVersion=1.5.1
 | 
			
		||||
buildCount=8
 | 
			
		||||
buildCount=41
 | 
			
		||||
baseBetaVersion=1.5.2
 | 
			
		||||
 
 | 
			
		||||
@@ -45,6 +45,22 @@
 | 
			
		||||
 | 
			
		||||
        </service>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        <service android:name=".services.MainService"
 | 
			
		||||
            android:exported="true"/>
 | 
			
		||||
 | 
			
		||||
        <service android:name=".services.AssistantService"/>
 | 
			
		||||
        
 | 
			
		||||
        <receiver android:name="cc.winboll.studio.appbase.receivers.MainReceiver">
 | 
			
		||||
 | 
			
		||||
            <intent-filter>
 | 
			
		||||
 | 
			
		||||
                <action android:name="cc.winboll.studio.appbase.receivers.MainReceiver"/>
 | 
			
		||||
 | 
			
		||||
            </intent-filter>
 | 
			
		||||
 | 
			
		||||
        </receiver>
 | 
			
		||||
        
 | 
			
		||||
        <meta-data
 | 
			
		||||
            android:name="android.max_aspect"
 | 
			
		||||
            android:value="4.0"/>
 | 
			
		||||
 
 | 
			
		||||
@@ -8,11 +8,11 @@ import android.widget.CheckBox;
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity;
 | 
			
		||||
import androidx.appcompat.widget.Toolbar;
 | 
			
		||||
import cc.winboll.studio.appbase.R;
 | 
			
		||||
import cc.winboll.studio.appbase.services.MainService;
 | 
			
		||||
import cc.winboll.studio.libappbase.GlobalApplication;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogUtils;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogView;
 | 
			
		||||
import cc.winboll.studio.libappbase.SOS;
 | 
			
		||||
import cc.winboll.studio.libappbase.SOSCSBroadcastReceiver;
 | 
			
		||||
import cc.winboll.studio.libappbase.SimpleOperateSignalCenterService;
 | 
			
		||||
import com.hjq.toast.ToastUtils;
 | 
			
		||||
 | 
			
		||||
@@ -50,11 +50,11 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void onStartCenter(View view) {
 | 
			
		||||
        SimpleOperateSignalCenterService.startISOSService(this);
 | 
			
		||||
        MainService.startMainService(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void onStopCenter(View view) {
 | 
			
		||||
        SimpleOperateSignalCenterService.stopISOSService(this);
 | 
			
		||||
        MainService.stopMainService(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void onTestStopWithoutSettingEnable(View view) {
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,68 @@
 | 
			
		||||
package cc.winboll.studio.appbase.beans;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/13 07:06:13
 | 
			
		||||
 */
 | 
			
		||||
import android.util.JsonReader;
 | 
			
		||||
import android.util.JsonWriter;
 | 
			
		||||
import cc.winboll.studio.libappbase.BaseBean;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
public class MainServiceBean extends BaseBean {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "MainServiceBean";
 | 
			
		||||
 | 
			
		||||
    boolean isEnable;
 | 
			
		||||
 | 
			
		||||
    public MainServiceBean() {
 | 
			
		||||
        this.isEnable = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setIsEnable(boolean isEnable) {
 | 
			
		||||
        this.isEnable = isEnable;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isEnable() {
 | 
			
		||||
        return isEnable;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        return MainServiceBean.class.getName();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
 | 
			
		||||
        super.writeThisToJsonWriter(jsonWriter);
 | 
			
		||||
        MainServiceBean bean = this;
 | 
			
		||||
        jsonWriter.name("isEnable").value(bean.isEnable());
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
 | 
			
		||||
        if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
 | 
			
		||||
            if (name.equals("isEnable")) {
 | 
			
		||||
                setIsEnable(jsonReader.nextBoolean());
 | 
			
		||||
            } else {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
 | 
			
		||||
        jsonReader.beginObject();
 | 
			
		||||
        while (jsonReader.hasNext()) {
 | 
			
		||||
            String name = jsonReader.nextName();
 | 
			
		||||
            if (!initObjectsFromJsonReader(jsonReader, name)) {
 | 
			
		||||
                jsonReader.skipValue();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // 结束 JSON 对象
 | 
			
		||||
        jsonReader.endObject();
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,38 @@
 | 
			
		||||
package cc.winboll.studio.appbase.handlers;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/14 03:51:40
 | 
			
		||||
 */
 | 
			
		||||
import android.os.Handler;
 | 
			
		||||
import android.os.Message;
 | 
			
		||||
import cc.winboll.studio.appbase.services.MainService;
 | 
			
		||||
import java.lang.ref.WeakReference;
 | 
			
		||||
 | 
			
		||||
public class MainServiceHandler extends Handler {
 | 
			
		||||
    public static final String TAG = "MainServiceHandler";
 | 
			
		||||
 | 
			
		||||
    public static final int MSG_REMINDTHREAD = 0;
 | 
			
		||||
 | 
			
		||||
    WeakReference<MainService> serviceWeakReference;
 | 
			
		||||
    public MainServiceHandler(MainService service) {
 | 
			
		||||
        serviceWeakReference = new WeakReference<MainService>(service);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handleMessage(Message msg) {
 | 
			
		||||
        switch (msg.what) {
 | 
			
		||||
            case MSG_REMINDTHREAD: // 处理下载完成消息,更新UI
 | 
			
		||||
                {
 | 
			
		||||
                    // 显示提醒消息
 | 
			
		||||
                    //
 | 
			
		||||
                    //LogUtils.d(TAG, "显示提醒消息");
 | 
			
		||||
                    MainService mainService = serviceWeakReference.get();
 | 
			
		||||
                    if (mainService != null) {
 | 
			
		||||
                        mainService.appenMessage((String)msg.obj);
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,48 @@
 | 
			
		||||
package cc.winboll.studio.appbase.receivers;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/13 06:58:04
 | 
			
		||||
 * @Describe 主要广播接收器
 | 
			
		||||
 */
 | 
			
		||||
import android.content.BroadcastReceiver;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.content.IntentFilter;
 | 
			
		||||
import cc.winboll.studio.appbase.services.MainService;
 | 
			
		||||
import com.hjq.toast.ToastUtils;
 | 
			
		||||
import java.lang.ref.WeakReference;
 | 
			
		||||
 | 
			
		||||
public class MainReceiver extends BroadcastReceiver {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "MainReceiver";
 | 
			
		||||
    public static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
 | 
			
		||||
    WeakReference<MainService> mwrService;
 | 
			
		||||
    // 存储电量指示值,
 | 
			
		||||
    // 用于校验电量消息时的电量变化
 | 
			
		||||
    static volatile int _mnTheQuantityOfElectricityOld = -1;
 | 
			
		||||
    static volatile boolean _mIsCharging = false;
 | 
			
		||||
 | 
			
		||||
    public MainReceiver(MainService service) {
 | 
			
		||||
        mwrService = new WeakReference<MainService>(service);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onReceive(Context context, Intent intent) {
 | 
			
		||||
        String szAction = intent.getAction();
 | 
			
		||||
        if (szAction.equals(ACTION_BOOT_COMPLETED)) {
 | 
			
		||||
            ToastUtils.show("ACTION_BOOT_COMPLETED");
 | 
			
		||||
        } else {
 | 
			
		||||
            ToastUtils.show(szAction);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 注册 Receiver
 | 
			
		||||
    //
 | 
			
		||||
    public void registerAction(Context context) {
 | 
			
		||||
        IntentFilter filter=new IntentFilter();
 | 
			
		||||
        filter.addAction(ACTION_BOOT_COMPLETED);
 | 
			
		||||
        //filter.addAction(Intent.ACTION_BATTERY_CHANGED);
 | 
			
		||||
        context.registerReceiver(this, filter);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,100 @@
 | 
			
		||||
package cc.winboll.studio.appbase.services;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/14 03:38:31
 | 
			
		||||
 * @Describe 守护进程服务
 | 
			
		||||
 */
 | 
			
		||||
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 cc.winboll.studio.appbase.beans.MainServiceBean;
 | 
			
		||||
import cc.winboll.studio.appbase.services.AssistantService;
 | 
			
		||||
import cc.winboll.studio.appbase.services.MainService;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogUtils;
 | 
			
		||||
 | 
			
		||||
public class AssistantService extends Service {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "AssistantService";
 | 
			
		||||
 | 
			
		||||
    MainServiceBean mMainServiceBean;
 | 
			
		||||
    MyServiceConnection mMyServiceConnection;
 | 
			
		||||
    volatile boolean mIsThreadAlive;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public IBinder onBind(Intent intent) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onCreate() {
 | 
			
		||||
        LogUtils.d(TAG, "onCreate");
 | 
			
		||||
        super.onCreate();
 | 
			
		||||
 | 
			
		||||
        //mMyBinder = new MyBinder();
 | 
			
		||||
        if (mMyServiceConnection == null) {
 | 
			
		||||
            mMyServiceConnection = new MyServiceConnection();
 | 
			
		||||
        }
 | 
			
		||||
        // 设置运行参数
 | 
			
		||||
        mIsThreadAlive = false;
 | 
			
		||||
        assistantService();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int onStartCommand(Intent intent, int flags, int startId) {
 | 
			
		||||
        LogUtils.d(TAG, "call onStartCommand(...)");
 | 
			
		||||
        assistantService();
 | 
			
		||||
        return START_STICKY;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onDestroy() {
 | 
			
		||||
        //LogUtils.d(TAG, "onDestroy");
 | 
			
		||||
        mIsThreadAlive = false;
 | 
			
		||||
        super.onDestroy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 运行服务内容
 | 
			
		||||
    //
 | 
			
		||||
    void assistantService() {
 | 
			
		||||
        LogUtils.d(TAG, "assistantService()");
 | 
			
		||||
        mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class);
 | 
			
		||||
        if (mMainServiceBean.isEnable()) {
 | 
			
		||||
            if (mIsThreadAlive == false) {
 | 
			
		||||
                // 设置运行状态
 | 
			
		||||
                mIsThreadAlive = true;
 | 
			
		||||
                // 唤醒和绑定主进程
 | 
			
		||||
                wakeupAndBindMain();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 唤醒和绑定主进程
 | 
			
		||||
    //
 | 
			
		||||
    void wakeupAndBindMain() {
 | 
			
		||||
        LogUtils.d(TAG, "wakeupAndBindMain()");
 | 
			
		||||
        startService(new Intent(this, MainService.class));
 | 
			
		||||
        bindService(new Intent(AssistantService.this, MainService.class), mMyServiceConnection, Context.BIND_IMPORTANT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 主进程与守护进程连接时需要用到此类
 | 
			
		||||
    //
 | 
			
		||||
    class MyServiceConnection implements ServiceConnection {
 | 
			
		||||
        @Override
 | 
			
		||||
        public void onServiceConnected(ComponentName name, IBinder service) {
 | 
			
		||||
            LogUtils.d(TAG, "onServiceConnected(...)");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void onServiceDisconnected(ComponentName name) {
 | 
			
		||||
            LogUtils.d(TAG, "onServiceDisconnected(...)");
 | 
			
		||||
            mMainServiceBean = MainServiceBean.loadBean(AssistantService.this, MainServiceBean.class);
 | 
			
		||||
            if (mMainServiceBean.isEnable()) {
 | 
			
		||||
                wakeupAndBindMain();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,219 @@
 | 
			
		||||
package cc.winboll.studio.appbase.services;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/13 06:56:41
 | 
			
		||||
 * @Describe 拨号主服务
 | 
			
		||||
 * 参考:
 | 
			
		||||
 * 进程保活-双进程守护的正确姿势
 | 
			
		||||
 * https://blog.csdn.net/sinat_35159441/article/details/75267380
 | 
			
		||||
 * Android Service之onStartCommand方法研究
 | 
			
		||||
 * https://blog.csdn.net/cyp331203/article/details/38920491
 | 
			
		||||
 */
 | 
			
		||||
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 cc.winboll.studio.appbase.beans.MainServiceBean;
 | 
			
		||||
import cc.winboll.studio.appbase.handlers.MainServiceHandler;
 | 
			
		||||
import cc.winboll.studio.appbase.receivers.MainReceiver;
 | 
			
		||||
import cc.winboll.studio.appbase.services.AssistantService;
 | 
			
		||||
import cc.winboll.studio.appbase.threads.MainServiceThread;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogUtils;
 | 
			
		||||
import cc.winboll.studio.libappbase.SOS;
 | 
			
		||||
import com.hjq.toast.ToastUtils;
 | 
			
		||||
 | 
			
		||||
public class MainService extends Service {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "MainService";
 | 
			
		||||
 | 
			
		||||
    public static final int MSG_UPDATE_STATUS = 0;
 | 
			
		||||
 | 
			
		||||
    static MainService _mControlCenterService;
 | 
			
		||||
 | 
			
		||||
    volatile boolean isServiceRunning;
 | 
			
		||||
 | 
			
		||||
    MainServiceBean mMainServiceBean;
 | 
			
		||||
    MainServiceThread mMainServiceThread;
 | 
			
		||||
    MainServiceHandler mMainServiceHandler;
 | 
			
		||||
    MyServiceConnection mMyServiceConnection;
 | 
			
		||||
    MainReceiver mMainReceiver;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public IBinder onBind(Intent intent) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MainServiceThread getRemindThread() {
 | 
			
		||||
        return mMainServiceThread;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onCreate() {
 | 
			
		||||
        super.onCreate();
 | 
			
		||||
        LogUtils.d(TAG, "onCreate()");
 | 
			
		||||
        _mControlCenterService = MainService.this;
 | 
			
		||||
        isServiceRunning = false;
 | 
			
		||||
        mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class);
 | 
			
		||||
 | 
			
		||||
        if (mMyServiceConnection == null) {
 | 
			
		||||
            mMyServiceConnection = new MyServiceConnection();
 | 
			
		||||
        }
 | 
			
		||||
        mMainServiceHandler = new MainServiceHandler(this);
 | 
			
		||||
 | 
			
		||||
        // 运行服务内容
 | 
			
		||||
        mainService();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int onStartCommand(Intent intent, int flags, int startId) {
 | 
			
		||||
        LogUtils.d(TAG, "onStartCommand(...)");
 | 
			
		||||
        // 运行服务内容
 | 
			
		||||
        mainService();
 | 
			
		||||
        return (mMainServiceBean.isEnable()) ? START_STICKY : super.onStartCommand(intent, flags, startId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 运行服务内容
 | 
			
		||||
    //
 | 
			
		||||
    void mainService() {
 | 
			
		||||
        LogUtils.d(TAG, "mainService()");
 | 
			
		||||
        mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class);
 | 
			
		||||
        if (mMainServiceBean.isEnable() && isServiceRunning == false) {
 | 
			
		||||
            LogUtils.d(TAG, "mainService() start running");
 | 
			
		||||
            isServiceRunning = true;
 | 
			
		||||
            // 唤醒守护进程
 | 
			
		||||
            wakeupAndBindAssistant();
 | 
			
		||||
 | 
			
		||||
            if (mMainReceiver == null) {
 | 
			
		||||
                // 注册广播接收器
 | 
			
		||||
                mMainReceiver = new MainReceiver(this);
 | 
			
		||||
                mMainReceiver.registerAction(this);
 | 
			
		||||
            }
 | 
			
		||||
            startMainServiceThread();
 | 
			
		||||
            LogUtils.i(TAG, "Main Service Is Start.");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 唤醒和绑定守护进程
 | 
			
		||||
    //
 | 
			
		||||
    void wakeupAndBindAssistant() {
 | 
			
		||||
        LogUtils.d(TAG, "wakeupAndBindAssistant()");
 | 
			
		||||
//        if (ServiceUtils.isServiceAlive(getApplicationContext(), AssistantService.class.getName()) == false) {
 | 
			
		||||
//            startService(new Intent(MainService.this, AssistantService.class));
 | 
			
		||||
//            //LogUtils.d(TAG, "call wakeupAndBindAssistant() : Binding... AssistantService");
 | 
			
		||||
//            bindService(new Intent(MainService.this, AssistantService.class), mMyServiceConnection, Context.BIND_IMPORTANT);
 | 
			
		||||
//        }
 | 
			
		||||
        Intent intent = new Intent(this, AssistantService.class);
 | 
			
		||||
        startService(intent);
 | 
			
		||||
        LogUtils.d(TAG, "startService(intent)");
 | 
			
		||||
        bindService(new Intent(this, AssistantService.class), mMyServiceConnection, Context.BIND_IMPORTANT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 开启提醒铃声线程
 | 
			
		||||
    //
 | 
			
		||||
    public void startMainServiceThread() {
 | 
			
		||||
        LogUtils.d(TAG, "startMainServiceThread");
 | 
			
		||||
        if (mMainServiceThread == null) {
 | 
			
		||||
            mMainServiceThread = new MainServiceThread(this, mMainServiceHandler);
 | 
			
		||||
            LogUtils.d(TAG, "new MainServiceThread");
 | 
			
		||||
        } else {
 | 
			
		||||
            if (mMainServiceThread.isExist() == true) {
 | 
			
		||||
                mMainServiceThread = new MainServiceThread(this, mMainServiceHandler);
 | 
			
		||||
                LogUtils.d(TAG, "renew MainServiceThread");
 | 
			
		||||
            } else {
 | 
			
		||||
                // 提醒进程正在进行中就更新状态后退出
 | 
			
		||||
                LogUtils.d(TAG, "A mMainServiceThread running.");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        mMainServiceThread.start();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void stopRemindThread() {
 | 
			
		||||
        if (mMainServiceThread != null) {
 | 
			
		||||
            mMainServiceThread.setIsExist(true);
 | 
			
		||||
            mMainServiceThread = null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onDestroy() {
 | 
			
		||||
        //LogUtils.d(TAG, "onDestroy");
 | 
			
		||||
        mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class);
 | 
			
		||||
        if (mMainServiceBean.isEnable() == false) {
 | 
			
		||||
            // 设置运行状态
 | 
			
		||||
            isServiceRunning = false;
 | 
			
		||||
            // 停止守护进程
 | 
			
		||||
            Intent intent = new Intent(this, AssistantService.class);
 | 
			
		||||
            stopService(intent);
 | 
			
		||||
            // 停止Receiver
 | 
			
		||||
            if (mMainReceiver != null) {
 | 
			
		||||
                unregisterReceiver(mMainReceiver);
 | 
			
		||||
                mMainReceiver = null;
 | 
			
		||||
            }
 | 
			
		||||
            // 停止前台通知栏
 | 
			
		||||
            stopForeground(true);
 | 
			
		||||
            // 停止消息提醒进程
 | 
			
		||||
            stopRemindThread();
 | 
			
		||||
            super.onDestroy();
 | 
			
		||||
            //LogUtils.d(TAG, "onDestroy done");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 主进程与守护进程连接时需要用到此类
 | 
			
		||||
    //
 | 
			
		||||
    private class MyServiceConnection implements ServiceConnection {
 | 
			
		||||
        @Override
 | 
			
		||||
        public void onServiceConnected(ComponentName name, IBinder service) {
 | 
			
		||||
            LogUtils.d(TAG, "onServiceConnected(...)");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void onServiceDisconnected(ComponentName name) {
 | 
			
		||||
            LogUtils.d(TAG, "onServiceDisconnected(...)");
 | 
			
		||||
            if (mMainServiceBean.isEnable()) {
 | 
			
		||||
                // 唤醒守护进程
 | 
			
		||||
                wakeupAndBindAssistant();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    //
 | 
			
		||||
//    // 启动服务
 | 
			
		||||
//    //
 | 
			
		||||
//    public static void startControlCenterService(Context context) {
 | 
			
		||||
//        Intent intent = new Intent(context, MainService.class);
 | 
			
		||||
//        context.startForegroundService(intent);
 | 
			
		||||
//    }
 | 
			
		||||
//
 | 
			
		||||
//    //
 | 
			
		||||
//    // 停止服务
 | 
			
		||||
//    //
 | 
			
		||||
//    public static void stopControlCenterService(Context context) {
 | 
			
		||||
//        Intent intent = new Intent(context, MainService.class);
 | 
			
		||||
//        context.stopService(intent);
 | 
			
		||||
//    }
 | 
			
		||||
 | 
			
		||||
    public void appenMessage(String message) {
 | 
			
		||||
        LogUtils.d(TAG, String.format("Message : %s", message));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void stopMainService(Context context) {
 | 
			
		||||
        LogUtils.d(TAG, "stopMainService");
 | 
			
		||||
        MainServiceBean bean = new MainServiceBean();
 | 
			
		||||
        bean.setIsEnable(false);
 | 
			
		||||
        MainServiceBean.saveBean(context, bean);
 | 
			
		||||
        context.stopService(new Intent(context, MainService.class));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void startMainService(Context context) {
 | 
			
		||||
        LogUtils.d(TAG, "startMainService");
 | 
			
		||||
        MainServiceBean bean = new MainServiceBean();
 | 
			
		||||
        bean.setIsEnable(true);
 | 
			
		||||
        MainServiceBean.saveBean(context, bean);
 | 
			
		||||
        context.startService(new Intent(context, MainService.class));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,51 @@
 | 
			
		||||
package cc.winboll.studio.appbase.threads;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/14 03:46:44
 | 
			
		||||
 */
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import cc.winboll.studio.appbase.handlers.MainServiceHandler;
 | 
			
		||||
import cc.winboll.studio.libappbase.LogUtils;
 | 
			
		||||
import java.lang.ref.WeakReference;
 | 
			
		||||
 | 
			
		||||
public class MainServiceThread extends Thread {
 | 
			
		||||
 | 
			
		||||
    public static final String TAG = "MainServiceThread";
 | 
			
		||||
 | 
			
		||||
    Context mContext;
 | 
			
		||||
 | 
			
		||||
    // 控制线程是否退出的标志
 | 
			
		||||
    volatile boolean isExist = false;
 | 
			
		||||
 | 
			
		||||
    // 服务Handler, 用于线程发送消息使用
 | 
			
		||||
    WeakReference<MainServiceHandler> mwrMainServiceHandler;
 | 
			
		||||
 | 
			
		||||
    public void setIsExist(boolean isExist) {
 | 
			
		||||
        this.isExist = isExist;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isExist() {
 | 
			
		||||
        return isExist;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MainServiceThread(Context context, MainServiceHandler handler) {
 | 
			
		||||
        mContext = context;
 | 
			
		||||
        mwrMainServiceHandler = new WeakReference<MainServiceHandler>(handler);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void run() {
 | 
			
		||||
        LogUtils.d(TAG, "run()");
 | 
			
		||||
        while (!isExist()) {
 | 
			
		||||
            //LogUtils.d(TAG, "run()");
 | 
			
		||||
            try {
 | 
			
		||||
                Thread.sleep(1000);
 | 
			
		||||
            } catch (InterruptedException e) {
 | 
			
		||||
                LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        LogUtils.d(TAG, "run() exit.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
#Created by .winboll/winboll_app_build.gradle
 | 
			
		||||
#Thu Feb 13 23:29:49 GMT 2025
 | 
			
		||||
#Fri Feb 14 13:15:46 GMT 2025
 | 
			
		||||
stageCount=2
 | 
			
		||||
libraryProject=libappbase
 | 
			
		||||
baseVersion=1.5
 | 
			
		||||
publishVersion=1.5.1
 | 
			
		||||
buildCount=8
 | 
			
		||||
buildCount=41
 | 
			
		||||
baseBetaVersion=1.5.2
 | 
			
		||||
 
 | 
			
		||||
@@ -22,14 +22,6 @@
 | 
			
		||||
            android:exported="true">
 | 
			
		||||
        </service>
 | 
			
		||||
        
 | 
			
		||||
        <service android:name="cc.winboll.studio.libappbase.SOSService"
 | 
			
		||||
            android:exported="true">
 | 
			
		||||
            <intent-filter>
 | 
			
		||||
                <action android:name="cc.winboll.studio.libappbase.SOSService" />
 | 
			
		||||
            </intent-filter>
 | 
			
		||||
        </service>
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        <activity android:name=".LogActivity"/>
 | 
			
		||||
 | 
			
		||||
        <receiver android:name=".receiver.MyBroadcastReceiver">
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +0,0 @@
 | 
			
		||||
package cc.winboll.studio.libappbase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Author ZhanGSKen@AliYun.Com
 | 
			
		||||
 * @Date 2025/02/14 05:39:44
 | 
			
		||||
 */
 | 
			
		||||
import android.app.Service;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.os.IBinder;
 | 
			
		||||
import android.os.RemoteException;
 | 
			
		||||
 | 
			
		||||
public class SOSService extends Service {
 | 
			
		||||
    
 | 
			
		||||
    public static final String TAG = "SOSService";
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public IBinder onBind(Intent intent) {
 | 
			
		||||
        
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
@@ -10,7 +10,11 @@ import android.app.Service;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.os.IBinder;
 | 
			
		||||
import android.os.IInterface;
 | 
			
		||||
import android.os.Parcel;
 | 
			
		||||
import android.os.RemoteException;
 | 
			
		||||
import cc.winboll.studio.libappbase.bean.SimpleOperateSignalCenterServiceBean;
 | 
			
		||||
import java.io.FileDescriptor;
 | 
			
		||||
 | 
			
		||||
public class SimpleOperateSignalCenterService extends Service {
 | 
			
		||||
 | 
			
		||||
@@ -18,6 +22,8 @@ public class SimpleOperateSignalCenterService extends Service {
 | 
			
		||||
    public static final String ACTION_ENABLE = SimpleOperateSignalCenterService.class.getName() + ".ACTION_ENABLE";
 | 
			
		||||
    public static final String ACTION_DISABLE = SimpleOperateSignalCenterService.class.getName() + ".ACTION_DISABLE";
 | 
			
		||||
    
 | 
			
		||||
    private final IBinder binder =(IBinder)new SOSBinder();
 | 
			
		||||
    
 | 
			
		||||
    SimpleOperateSignalCenterServiceBean mSimpleOperateSignalCenterServiceBean;
 | 
			
		||||
    static MainThread _MainThread;
 | 
			
		||||
    public static synchronized MainThread getMainThreadInstance() {
 | 
			
		||||
@@ -29,9 +35,59 @@ public class SimpleOperateSignalCenterService extends Service {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public IBinder onBind(Intent intent) {
 | 
			
		||||
        return binder;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public class SOSBinder implements IBinder {
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void dump(FileDescriptor fileDescriptor, String[] string) throws RemoteException {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void dumpAsync(FileDescriptor fileDescriptor, String[] string) throws RemoteException {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public String getInterfaceDescriptor() throws RemoteException {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public boolean isBinderAlive() {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void linkToDeath(IBinder.DeathRecipient deathRecipient, int p) throws RemoteException {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public boolean pingBinder() {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public IInterface queryLocalInterface(String string) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public boolean transact(int p, Parcel parcel, Parcel parcel1, int p1) throws RemoteException {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public boolean unlinkToDeath(IBinder.DeathRecipient deathRecipient, int p) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public static final String TAG = "SOSBinder";
 | 
			
		||||
        SimpleOperateSignalCenterService getService() {
 | 
			
		||||
            return SimpleOperateSignalCenterService.this;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onCreate() {
 | 
			
		||||
        super.onCreate();
 | 
			
		||||
@@ -95,6 +151,10 @@ public class SimpleOperateSignalCenterService extends Service {
 | 
			
		||||
        context.startService(new Intent(context, SimpleOperateSignalCenterService.class));
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public String getMessage() {
 | 
			
		||||
        return "Hello from SimpleOperateSignalCenterService";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static class MainThread extends Thread {
 | 
			
		||||
        volatile boolean isExist = false;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user