RemoteService.java 2.57 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.ali.MonitorService;
import com.lotus.town.config.SupportAction;
import com.secure.clean.ali.service.IMyAidlInterface;

public class RemoteService extends Service {

    private MyBinder mBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
        }

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

    public RemoteService() {
    }

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

    private boolean stop = false;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(stop) {
                monitorScreenOn();
                handler.sendEmptyMessageDelayed(0, 5000);
            }
        }
    };

    private void monitorScreenOn(){

        if (((PowerManager) this.getSystemService(Context.POWER_SERVICE)).isScreenOn()) {
        } else {
            Intent i = new Intent();
                i.setPackage(this.getPackageName());
                i.setAction(SupportAction.SCREEN_OFF_MY);
                this.sendBroadcast(i);
            stop = true;

        }
    }


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

    private class MyBinder extends IMyAidlInterface.Stub{

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

    }
}