package com.mzeat;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.mzeat.api.IMzeatService;
import com.mzeat.api.MzeatService;
import com.mzeat.image.ImageCache;
import com.mzeat.util.StringUtils;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.widget.Toast;
public class MzeatApplication extends Application {
private static MzeatApplication instance;
public static ImageCache mImageCache;
private IMzeatService service;
private PreferencesConfig mPreferencesConfig;
public boolean m_bKeyRight = true;
public static boolean init_result = false;
public BMapManager mBMapManager = null;
public static final String strKey = "1FEB4D697C458BF982CFBC2DA1B6EB4EFD72A728";
public LocationClient mLocationClient = null;
public MyLocationListenner myListener = new MyLocationListenner();
public static final int PAGE_SIZE = 20;//默认分页大小
public static MzeatApplication getInstance() {
return instance;
}
public String lon = "";
public String lat = "";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
//初始化实例
instance = this;
//初始化API接口服务
service = new MzeatService();
//初始化设置
mPreferencesConfig = new PreferencesConfig(this);
//初始化百度地图
initEngineManager(this);
//初始化位置服务,设置位置监听
mLocationClient = new LocationClient(this);
mLocationClient.registerLocationListener(myListener);
//初始化推送
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
//设置推送的通知样式,PS:貌似不起作用。
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
this, R.layout.customer_notitfication_layout, R.id.icon,
R.id.title, R.id.text);
builder.layoutIconDrawable = R.drawable.ic_launcher;
builder.developerArg0 = "developerArg2";
JPushInterface.setPushNotificationBuilder(2, builder);
}
public IMzeatService getService() {
return service;
}
public PreferencesConfig getpPreferencesConfig() {
return mPreferencesConfig;
}
public void initEngineManager(Context context) {
if (mBMapManager == null) {
mBMapManager = new BMapManager(context);
}
while (!init_result) {
init_result = mBMapManager.init(strKey, new MyGeneralListener());
Log.e("MzeatApplication.getInstance().init_result",
String.valueOf(init_result));
}
}
@Override
// 建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
public void onTerminate() {
// TODO Auto-generated method stub
if (mBMapManager != null) {
mBMapManager.destroy();
mBMapManager = null;
}
super.onTerminate();
}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
public static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Log.e("onGetNetworkState", String.valueOf(iError));
if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
Log.e("iError", String.valueOf(iError));
init_result = false;
Toast.makeText(
MzeatApplication.getInstance().getApplicationContext(),
"您的网络出错啦!", Toast.LENGTH_LONG).show();
} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
Toast.makeText(
MzeatApplication.getInstance().getApplicationContext(),
"输入正确的检索条件!", Toast.LENGTH_LONG).show();
}
// ...
}
@Override
public void onGetPermissionState(int iError) {
Log.e("onGetPermissionState", String.valueOf(iError));
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授权Key错误:
Toast.makeText(
MzeatApplication.getInstance().getApplicationContext(),
"输入正确的授权Key!", Toast.LENGTH_LONG).show();
MzeatApplication.getInstance().m_bKeyRight = false;
}
}
}
/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
//设置纬度
StringBuffer sb1 = new StringBuffer(256);
lat = sb1.append(location.getLatitude()).toString();
// Log.e("location", sb1.toString());
//设置经度
StringBuffer sb2 = new StringBuffer(256);
lon = sb2.append(location.getLongitude()).toString();
// Log.e("location", sb2.toString());
/**
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
// Log.e("latitude", String.valueOf(location.getLatitude()));
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
// sb.append("\n省:");
// sb.append(location.getProvince());
// sb.append("\n市:");
// sb.append(location.getCity());
// sb.append("\n区/县:");
// sb.append(location.getDistrict());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
sb.append("\nsdk version : ");
sb.append(mLocationClient.getVersion());
sb.append("\nisCellChangeFlag : ");
sb.append(location.isCellChangeFlag());
// Log.e("location", sb.toString());
*
*/
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
/**
* 获取App安装包信息
*
* @return
*/
public PackageInfo getPackageInfo() {
PackageInfo info = null;
try {
info = getPackageManager().getPackageInfo(getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace(System.err);
}
if (info == null)
info = new PackageInfo();
return info;
}
public void setProperty(String key, String value) {
AppConfig.getAppConfig(this).set(key, value);
}
public String getProperty(String key) {
return AppConfig.getAppConfig(this).get(key);
}
/**
* 判断当前版本是否兼容目标版本的方法
*
* @param VersionCode
* @return
*/
public static boolean isMethodsCompat(int VersionCode) {
int currentVersion = android.os.Build.VERSION.SDK_INT;
return currentVersion >= VersionCode;
}
/**
* 是否启动检查更新
*
* @return
*/
public boolean isCheckUp() {
String perf_checkup = getProperty(AppConfig.CONF_CHECKUP);
// 默认是开启
if (StringUtils.isEmpty(perf_checkup))
return true;
else
return StringUtils.toBool(perf_checkup);
}
}