package com.ectrip.cyt.ui; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.atomic.AtomicBoolean; import android.annotation.SuppressLint; import android.app.Dialog; import android.app.Service; import android.content.Context; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.support.v4.app.FragmentActivity; import android.view.View; import android.widget.Toast; import com.ectrip.cyt.config.BindService; import com.ectrip.cyt.config.DevicTool; import com.ectrip.cyt.config.MyApp; import com.ectrip.cyt.constant.constant; import com.ectrip.cyt.shield_home.LockLayer; import com.ectrip.cyt.shield_home.LockLayer.MToast; import com.ectrip.cyt.utils.LogUtil; import com.ectrip.trips.check.R; /** * @author jigo activity父类 */ public abstract class BaseActivity extends FragmentActivity { public final String TAG = "BaseActivity"; private boolean isTablet; public final int NETSUCESS = 88; public final int NETFAILRE = 89; public final int NETDIALOG = 90; public NetHandler netHandler; public AtomicBoolean isStop = new AtomicBoolean(false); public PowerManager powerManager = null; public WakeLock wakeLock = null; private AtomicBoolean isPause = new AtomicBoolean(false); private Timer timer; public Dialog netDialog; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); isTablet = DevicTool.getInstance().isTablet(this); MyApp.getInstance().addActivity(this); if (netHandler == null) { netHandler = new NetHandler(); } try { powerManager = (PowerManager) this .getSystemService(Service.POWER_SERVICE); wakeLock = this.powerManager.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK, "My Lock"); // 是否需计算锁的数量 wakeLock.setReferenceCounted(false); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } // 请求常亮,onResume() try { if (wakeLock != null) { wakeLock.acquire(); } } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } /** * 设置为横屏 */ LogUtil.i(TAG, "isTablet == " + isTablet); if (isTablet) { if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else { if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @Override protected void onResume() { super.onResume(); initNet(); } protected void initNet() { timer = new Timer(); isPause.set(false); TimerTask task = new TimerTask() { @Override public void run() { if (!MyApp.getInstance().checkNetwork() && !isPause.get()) { try { netHandler.sendMessage(netHandler .obtainMessage(NETDIALOG)); } catch (Exception e) { e.printStackTrace(); } } } }; timer.schedule(task, 4000);// 2秒后启动任务 new Thread(new Runnable() { @Override public void run() { while (!isStop.get()) { if (MyApp.getInstance().checkNetwork()) { isPause.set(true); netHandler.sendMessage(netHandler .obtainMessage(NETSUCESS)); } sleep(800); } } }).start(); } @SuppressLint("HandlerLeak") public class NetHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case NETSUCESS: if (netDialog != null && netDialog.isShowing()) { try { netDialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } MToast(BaseActivity.this, getString(R.string.connect_net), MToast.LENGTH_SHORT); } break; case NETDIALOG: if (netDialog == null) { netDialog = MyApp.getInstance().initNetWork( BaseActivity.this); } else if (!netDialog.isShowing()) { netDialog.show(); } break; default: break; } } } private void sleep(int ms) { try { java.lang.Thread.sleep(ms); } catch (Exception e) { e.printStackTrace(); } } public static Context getBasecontext() { return BaseActivity.getBasecontext(); } /* * ...........................屏蔽home键.................................. */ public LockLayer lockLayer; private View lock; public boolean isHandset; public void setContentView(int layoutResID) { isHandset = ((MyApp) getApplication()).isHandset; if (isHandset) { lock = View.inflate(this, layoutResID, null); lockLayer = new LockLayer(this); lockLayer.setLockView(lock); lockLayer.lock(); } else { super.setContentView(layoutResID); } } public View findViewById(int id) { if (isHandset) { return lock.findViewById(id); } else { return super.findViewById(id); } } private long lastClick; public void MToast(Context context, CharSequence charSequence, int duration) { try { if (charSequence == null) { return; } if (System.currentTimeMillis() - lastClick <= constant.mToastTime) { return; } lastClick = System.currentTimeMillis(); if (isHandset) { lockLayer.setToast(context, charSequence, duration); } else { Toast.makeText(context, charSequence, duration).show(); } } catch (Exception e) { e.printStackTrace(); } } @Override public void onBackPressed() { if (timer != null) { timer.cancel(); } isPause.set(true); super.onBackPressed(); LogUtil.i(TAG,getString(R.string.back_pressed)); if (netDialog != null && netDialog.isShowing()) { try { netDialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } } } @Override protected void onPause() { if (timer != null) { timer.cancel(); } isPause.set(true); super.onPause(); // 取消屏幕常亮,onPause() try { if (wakeLock != null) { wakeLock.release(); } } catch (Exception e) { e.printStackTrace(); } if (netDialog != null && netDialog.isShowing()) { try { netDialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } } } /***************************** 2.0 *************************************/ @Override protected void onDestroy() { if (timer != null) { timer.cancel(); } if (((MyApp) getApplication()).isHandset) { lockLayer.unlock(); } MyApp.getInstance().removeActivity(this); isPause.set(true); isStop.set(true); super.onDestroy(); if (netDialog != null && netDialog.isShowing()) { try { netDialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } } } }