package com.imofan.android.basic.notification;
import java.util.Date;

import com.imofan.android.basic.LogUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
/**
 * 定时广播接收器
 * @author liyang
 *
 */
public class MFAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //推送开关状态
        boolean pushStatus = MFPreferencesUtils.getPreference(context, "push", "status", false);
        //Log.v("MFAlarmReceiver", "pushStatus :"+pushStatus);
        LogUtils.writeLog("======推送服务广播接收器  pushStatus ："+pushStatus);
        if(pushStatus){
            //如果是晚上11点钟之后和早上7点钟之前，不检测最新推送
            int time = new Date(System.currentTimeMillis()).getHours();
            if(time>23||time<7){
                return;
            }
            
            //如果是自启动，设置定时器
            if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
                String listenerName = MFPreferencesUtils.getPreference(context, "mfpush", "pushListener", "");
                if(null!=listenerName && !"".equals(listenerName)){
                    try {
                    	LogUtils.writeLog("======开启启动推送广播接收器  pushStatus ："+pushStatus);
                        MFPNService.startPushService(context, Class.forName(listenerName));
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            //开启消息推送服务
            Intent messageIntent = new Intent(context,MFPNService.class);
            //3.1之后
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1){
            	intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            }
            context.startService(messageIntent);
        }
    }
}
