Newer
Older
dxcgt / app / src / main / java / com / smartdot / cgt / activity / BaseActivity.java
wangxitong on 6 Apr 2021 7 KB first commit
package com.smartdot.cgt.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

import com.smartdot.cgt.R;
import com.smartdot.cgt.util.ApplicationMain;
import com.smartdot.cgt.util.Msg;
import com.smartdot.cgt.util.NotificationCreater;
import com.smartdot.cgt.util.config.CgtConfig;

public abstract class BaseActivity extends Activity
{
	public static final String TAG = "BaseActivity";
	public static final String SOURCECASEKEY = "SOURCECASEKEY";
	public static final String MapReturn_PartId = "MapReturn_PartId";
	public static final String MapReturn_X = "MapReturn_X";
	public static final String MapReturn_Y = "MapReturn_Y";
	public static final String MapReturn_SheQu = "MapReturn_SheQu";
	public static final String MapReturn_SheQuId = "MapReturn_SheQuId";
	public static final String MapReturn_ShopId = "MapReturn_Shopid";
	public static final String MapReturn_ShopName = "MapReturn_ShopName";
	public static final String MapReturn_ShopTel = "MapReturn_ShopTel";
	public static final String MapReturn_LClass = "MapReturn_LClass";
	public static final String MapReturn_SClass = "MapReturn_SClass";
	public static final String MapReturn_GridId = "MapReturn_GridId";
	public static final String MapReturn_Address = "MapReturn_Address";
	public static final String MapReturn_Dept1 = "MapReturn_Dept1";
	public static final String MapReturn_Dept2 = "MapReturn_Dept2";
	public static final String MapReturn_Dept3 = "MapReturn_Dept3";
	public static final String KEY_NOTIFICATIONIDTOCLOSE = "NOTIFICATIONIDTOCLOSE";
	private ProgressDialog progressDialog = null;
	private boolean started = false;
	public static List<Activity> activedActivity = new ArrayList<Activity>();

	/**
	 * 消息处理句柄
	 */
	protected Handler baseHandler = new Handler()
	{

		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			BaseActivity.this.handleMessage(msg);
		}
	};

	public boolean isStarted()
	{
		return started;
	}

	@Override
	protected void onStart()
	{
		this.started = true;
		NotificationCreater.closeNotification(this, R.drawable.exe_icon);
		super.onStart();
		if (this instanceof FrmLogin)
		{
			activedActivity.clear();
		}
	}

	@Override
	protected void onStop()
	{
		this.started = false;
		super.onStop();
		// 当程序不在最前端显示,没有获得焦点,则发布一个通知栏消息
		boolean hasWindowFocus = activedActivity.size() == 0;
		FrmMain frmMain = null;
		for (Activity activity : activedActivity)
		{
			if (activity instanceof BaseActivity)
			{
				BaseActivity baseActivity = (BaseActivity) activity;
				if (activity.hasWindowFocus() || baseActivity.isStarted())
				{
					hasWindowFocus = true;
				}
			}
			if (activity instanceof FrmMain)
			{
				frmMain = (FrmMain) activity;
			}
		}
		if (!hasWindowFocus && !(this instanceof FrmLogin) && frmMain != null)
		{
			String tickerText = "城管通运行中";
			NotificationCreater.createNotificationOnlyLights(this,
					frmMain.getIntent(), R.drawable.exe_icon, tickerText,
					"城管通", tickerText);
		}
	}

	private void checkAppStatus() {
		if(AppStatusManager.getInstance().getAppStatus()==AppStatusManager.AppStatusConstant.APP_FORCE_KILLED) {
			//该应用已被回收
			//应用启动入口SplashActivity,走重启流程
			Intent intent = new Intent(this, FrmLogin.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.setTheme(getCgtConfig().getNowTheme());
		super.onCreate(savedInstanceState);
		BaseActivity.this.setLayout();
		if (!activedActivity.contains(this) && !(this instanceof FrmLogin))
		{
			activedActivity.add(this);
		}
		BaseActivity.this.addEventListener();
		BaseActivity.this.doInit();
		checkAppStatus();
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		BaseActivity.this.doDestroy();
		if (activedActivity.contains(this))
		{
			activedActivity.remove(this);
		}
	}

	protected void doDestroy()
	{

	}

	protected void doInit()
	{
	}

	/**
	 * 在此进行界面布局
	 */
	protected abstract void setLayout();

	/**
	 * 在此进行事件的绑定
	 */
	protected abstract void addEventListener();

	/**
	 * 线程返回消息处理
	 * 
	 * @param msg
	 *            消息内容
	 */
	protected abstract void handleMessage(Message msg);

	protected abstract void onCancelProgress(DialogInterface arg0);

	/**
	 * 彻底退出应用
	 */
	protected void finishAllActivity()
	{
		for (Activity activity : activedActivity)
		{
			if (activity instanceof FrmMain)
			{
				((FrmMain) activity).finishActivity(false);
			}
			else if (activity != this)
			{
				activity.finish();
			}
		}
		activedActivity.clear();
		this.finish();
	}

	/**
	 * 显示一个对话框
	 * 
	 * @param message
	 *            对话框内容
	 * @param title
	 *            对话框标题
	 */
	protected void showProgress(String message, String title)
	{
		if (progressDialog != null && progressDialog.isShowing())
		{
			progressDialog.setTitle(title);
			progressDialog.setMessage(message);
			return;
		}
		progressDialog = Msg.showProgressDialog(BaseActivity.this, message,
				title);
		progressDialog.setCancelable(true);
		progressDialog.setOnCancelListener(new OnCancelListener()
		{
			@Override
			public void onCancel(DialogInterface arg0)
			{
				BaseActivity.this.onCancelProgress(arg0);
			}
		});
	}

	/**
	 * 关闭一个对话框
	 */
	protected void closeProgress()
	{
		if (progressDialog != null)
		{
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	/**
	 * 初始化界面,去标题栏
	 */
	public void initActivity()
	{
		this.initActivity(getCgtConfig().isFullScreen());
	}

	/**
	 * 初始化界面,去标题栏,并根据参数确定是否全屏
	 * 
	 * @param fullScreen
	 */
	public void initActivity(boolean fullScreen)
	{

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		if (fullScreen)
		{
			this.getWindow().setFlags(
					WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
	}

	protected ApplicationMain getApplicationMain()
	{
		return ApplicationMain.getInstance();
	}

	protected CgtConfig getCgtConfig()
	{
		return getApplicationMain().getCgtConfig();
	}

//	protected DbHelper getDbHelper()
//	{
//		return getApplicationMain().getDbHelper();
//	}

	protected int getVersionCode()
	{
		int versionCode = 0;
		try
		{
			versionCode = getPackageManager().getPackageInfo(getPackageName(),
					0).versionCode;
		}
		catch (NameNotFoundException e)
		{
			e.printStackTrace();
		}
		return versionCode;
	}

	protected String getVersionName()
	{
		String versionName = "";
		try
		{
			versionName = getPackageManager().getPackageInfo(getPackageName(),
					0).versionName;
		}
		catch (NameNotFoundException e)
		{
			e.printStackTrace();
		}
		return versionName;
	}

	/**
	 * 删除通知
	 */
	protected void removeNotification()
	{
		if (this.getIntent() != null)
		{
			Bundle bundle = this.getIntent().getExtras();
			if (bundle != null)
			{
				int notificationToClose = bundle.getInt(
						KEY_NOTIFICATIONIDTOCLOSE, -1);
				if (notificationToClose > 0)
				{
					NotificationCreater.closeNotification(this,
							notificationToClose);
				}
			}
		}
	}
}