LocalService.java 2.63 KB
package com.lotus.town.ali.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException;

import com.lotus.town.config.SupportAction;
import com.sdk.Sdk;
import com.secure.clean.ali.service.IMyAidlInterface;

public class LocalService extends Service {
    private MyBinder mBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            startService(new Intent(LocalService.this,RemoteService.class));
            bindService(new Intent(LocalService.this,RemoteService.class),connection, Context.BIND_IMPORTANT);
        }
    };

    public LocalService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
    private boolean stop = false;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(stop) {
                monitorScreenOn();
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };
    private void monitorScreenOn(){
        if (((PowerManager) this.getSystemService(Context.POWER_SERVICE)).isScreenOn()) {
        } else {
            if(!Sdk.isInSplash) {
                Intent i = new Intent();
                i.setPackage(this.getPackageName());
                i.setAction(SupportAction.SCREEN_OFF_MY);
                this.sendBroadcast(i);
                stop = true;
            }
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        stop = false;
        if(handler.hasMessages(0)) {
            handler.removeMessages(0);
            handler.sendEmptyMessageDelayed(0, 3000);
        } else {
            handler.sendEmptyMessageDelayed(0, 3000);
        }
        startService(new Intent(LocalService.this,RemoteService.class));
        bindService(new Intent(this,RemoteService.class),connection, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        mBinder = new MyBinder();
        return mBinder;
    }

    private class MyBinder extends IMyAidlInterface.Stub{

        @Override
        public String getServiceName() throws RemoteException {
            return LocalService.class.getName();
        }

    }
}