MonitorService.java 4.61 KB
package com.lotus.town.ali;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;

import com.lotus.town.R;
import com.lotus.town.config.SupportAction;
import com.lotus.town.notify.NotificationUtils;
import com.lotus.town.notify.PushModel;
import com.sdk.Sdk;

import java.util.List;

public class MonitorService extends Service implements MediaPlayer.OnPreparedListener {

    private static boolean isAlive = false;

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

    @Override
    public void onCreate() {
        super.onCreate();
        isAlive = true;
    }
    public static boolean isApplicationBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initNotification();
        }
        monitor();
        stop = true;
        return START_STICKY;
    }


//    Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            Toast.makeText(MonitorService.this,"handler",Toast.LENGTH_SHORT).show();
//            monitorScreenOn();
//            handler.sendEmptyMessageDelayed(0,10000);
//        }
//    };

    private boolean stop = false;
    private void monitor(){
        final long timeInterval = 10000;
        Runnable runnable = new Runnable() {
            public void run() {
                while (true) {
                    if(stop){
                        break;
                    }
                    monitorScreenOn();
                    try {
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    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;

            }
        }
    }

    public static void invoke(Context context){
        try {
//            if (!isAlive) {
                Intent i = new Intent();
                i.setClass(context, MonitorService.class);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(i);
                } else {
                    context.startService(i);
                }
//            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void invokeMp3(Context context,boolean isStart){
        try {
            Intent i = new Intent();
            i.setClass(context, MonitorService.class);
            i.putExtra("m_p",isStart?1:2);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(i);
            } else {
                context.startService(i);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initNotification() {
        PushModel pushModel = new PushModel();
        pushModel.setContent("荷花小镇持续赚钱中");
        pushModel.setId("111");
        pushModel.setTitle("荷花小镇");
        pushModel.setFlag(Notification.FLAG_ONGOING_EVENT|Notification.FLAG_FOREGROUND_SERVICE);

        NotificationUtils.init().createNotificationChannel(this);
        Notification notification = NotificationUtils.init().sendNotify(this, pushModel, "0", "0");
        startForeground(111,notification);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
}