package com.jqyd.jpush;
import java.util.ArrayList;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.jiuqi.mobile.lbs.intf.internal.LBSErrorCode;
import com.jiuqi.mobile.lbs.intf.pub.GisInfo;
import com.jiuqi.mobile.lbs.intf.pub.LBSInfo;
import com.jqyd.app.LocationUtils;
import com.jqyd.app.ShareMethod;
import com.jqyd.manager.GztAllList;
import com.jqyd.manager.MainActivity;
import com.jqyd.model.LocForJpush;
import com.jqyd.model.LocationModule;
import com.jqyd.model.RecordModel;
import com.jqyd.shareInterface.Baidu_location;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.shareInterface.UpdataToServer;
import com.jqyd.utils.UUID;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;
/**
* 自定义接收器
*
* 如果不定义这个 Receiver,则: 1) 默认用户会打开主界面 2) 接收不到自定义消息
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Bundle bundle = intent.getExtras();
Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: "
+ printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle
.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "接收Registration Id : " + regId);
// send the Registration Id to your server...
} else if (JPushInterface.ACTION_UNREGISTER.equals(intent.getAction())) {
String regId = bundle
.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "接收UnRegistration Id : " + regId);
// send the UnRegistration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
.getAction())) {
Log.d(TAG,
"接收到推送下来的自定义消息: "
+ bundle.getString(JPushInterface.EXTRA_MESSAGE));
new ShareMethod(context).recordLog("接收到推送下来的自定义消息: "
+ bundle.getString(JPushInterface.EXTRA_MESSAGE));
// processCustomMessage(context, bundle);
String str = bundle.getString(JPushInterface.EXTRA_MESSAGE);
Gson gson = new Gson();
LocForJpush forJpush = gson.fromJson(str, LocForJpush.class);
if(forJpush!=null){
String url="";
if(forJpush.getName()!=null&&forJpush.getName().equals("com.jqyd.eip.push.Location")){
url = forJpush.getUrl();
UpLoad(url);
}
}
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
.getAction())) {
Log.d(TAG, "接收到推送下来的通知");
int notifactionId = bundle
.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
.getAction())) {
Log.d(TAG, "用户点击打开了通知");
// 打开自定义的Activity
// Intent i = new Intent(context, TestActivity.class);
// i.putExtras(bundle);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
Intent i = new Intent(context, GztAllList.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent
.getAction())) {
Log.d(TAG,
"用户收到到RICH PUSH CALLBACK: "
+ bundle.getString(JPushInterface.EXTRA_EXTRA));
// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity,
// 打开一个网页等..
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void UpLoad(final String url) {
new locTask(url).execute();
// new Thread() {
//
// @Override
// public void run() {
// Looper.prepare();
// Baidu_location baidu = new Baidu_location(context);
// // 获取基站相关信息
// LocationModule loc = new LocationUtils(context).takeCellInfos();
//
// LocationUtils locationUtils = new LocationUtils(context);
// boolean b = locationUtils.getLocation(loc, "baidu", "", baidu);
// if (b) {
// // 获取到位置
// // 调用位置上报方法
//// upLocation(loc,url);
// }
// Looper.myLooper();
// }
//
// }.start();
}
class locTask extends AsyncTask<Void, Void, Void>{
private String url;
public locTask(String url) {
this.url = url;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Looper.prepare();
Baidu_location baidu = new Baidu_location(context);
// 获取基站相关信息
LocationModule loc = new LocationUtils(context).takeCellInfos();
LocationUtils locationUtils = new LocationUtils(context);
boolean b = locationUtils.getLocation(loc, "baidu", "", baidu);
if (b) {
// 获取到位置
// 调用位置上报方法
upLocation(loc,url);
}
Looper.loop();
return null;
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
// send msg to MainActivity
// private void processCustomMessage(Context context, Bundle bundle) {
// if (MainActivity.isForeground) {
// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
// if (!ExampleUtil.isEmpty(extras)) {
// try {
// JSONObject extraJson = new JSONObject(extras);
// if (null != extraJson && extraJson.length() > 0) {
// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
// }
// } catch (JSONException e) {
//
// }
//
// }
// context.sendBroadcast(msgIntent);
// }
// }
/**
* 上传位置信息
*
* @param loc
* @param dw_type
* @return
*/
public String upLocation(LocationModule loc,String url) {
String result = "-1";
// Optdb_interfce db = new Optdb_interfce(context);
Optsharepre_interface share_obj = new Optsharepre_interface(context);
// 获得登录者姓名
// String userName = db.searchEmpName(share_obj.getDataFromPres("GUID"));
// db.close_SqlDb();
LBSInfo info = new LBSInfo();
info.setLatitude(loc.getLat());
info.setLongitude(loc.getLon());
info.setCellId(Integer.parseInt(loc.getCell_id()));
info.setMobileNumber(share_obj.getDataFromPres("REGSIM"));
info.setPosour(loc.getDw_type());
info.setRadius(Double.parseDouble(loc.getRadius()));
GisInfo gisInfo = new GisInfo();
info.setGisInfo(gisInfo);
LBSErrorCode code = new LBSErrorCode();
code.setCode(loc.getState());
switch (loc.getState()) {
case 1:
code.setDescribe("gps定位成功");
break;
case 2:
code.setDescribe("百度定位成功");
break;
case 4:
code.setDescribe("小区定位成功");
break;
case 110:
code.setDescribe("百度定位失败,wifi,gprs正常,gps打开");
break;
case 111:
code.setDescribe("gps定位失败,wifi,gprs正常,gps打开");
break;
case 120:
code.setDescribe("百度定位失败,wifi,gprs正常,gps关闭");
break;
case 121:
code.setDescribe("gps定位失败,wifi,gprs正常,gps关闭");
break;
case 130:
code.setDescribe("百度定位失败,wifi没联网,gprs正常,gps打开");
break;
case 131:
code.setDescribe("gps定位失败,wifi没联网,gprs正常,gps打开");
break;
case 140:
code.setDescribe("百度定位失败,wifi没联网,gprs正常,gps关闭");
break;
case 141:
code.setDescribe("gps定位失败,wifi没联网,gprs正常,gps关闭");
break;
case 150:
code.setDescribe("百度定位失败,wifi正常,gprs没网,gps打开");
break;
case 151:
code.setDescribe("gps定位失败,wifi正常,gprs没网,gps打开");
break;
case 160:
code.setDescribe("百度定位失败,wifi正常,gprs没网,gps关闭");
break;
case 161:
code.setDescribe("gps定位失败,wifi正常,gprs没网,gps关闭");
break;
case 170:
code.setDescribe("百度定位失败,wifi,gprs都没网,gps打开");
break;
case 171:
code.setDescribe("gps定位失败,wifi,gprs都没网,gps打开");
break;
case 180:
code.setDescribe("百度定位失败,wifi,gprs都没网,gps关闭");
break;
case 181:
code.setDescribe("gps定位失败,wifi,gprs都没网,gps关闭");
break;
default:
break;
}
info.setError(code);
info.setRecid(UUID.randomUUID().toString());
info.setUserId(share_obj.getDataFromPres("GUID"));
Gson gson = new Gson();
String json = gson.toJson(info);
// JSONObject object = new JSONObject();
// try {
// object.put("cosim", share_obj.getDataFromPres("COSIM"));
// object.put("regsim", share_obj.getDataFromPres("REGSIM"));
// object.put("time", loc.getTime());
// object.put("lon", loc.getLon());
// object.put("lat", loc.getLat());
// object.put("cell_id", loc.getCell_id());
// object.put("lac_code", loc.getLac());
// object.put("country_code", loc.getCcode());
// object.put("ncode", loc.getNcode());
// object.put("signal_strength", loc.getXhqd());
// object.put("loc_method", loc.getDw_type());
// object.put("radius", loc.getRadius());
// object.put("gguid", share_obj.getDataFromPres("GGUID"));
// object.put("guid", share_obj.getDataFromPres("GUID"));
// object.put("zdmc", userName);
// object.put("yys", loc.getYys());
//
// } catch (JSONException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
UpdataToServer server = new UpdataToServer(context);
// 请求服务器
// result = server.dataToServer("UPFORJPUSH", object);
result = server.dataToServerForURL(url, json);
// 记录日志
String log = "《位置上报》-----" + "定位方式:" + loc.getDw_type() + ",经度:"
+ loc.getLon() + ",纬度:" + loc.getLat() + ",上传结果:" + result;
Log.e("WZSB", log);
new ShareMethod(context).recordLog(log);
if (result != null && !result.equals("") && !result.equals("-1") && !result.equals("500") ) {
try {
Log.i("LGF", result);
JSONObject obj = new JSONObject(result);
String retMsg = obj.getString("detail");
result = retMsg;
} catch (JSONException e) {
e.printStackTrace();
}
} else {
// failToSave(object);
}
return result;
}
/**
* 上传失败后保存
*/
public void failToSave(final JSONObject object) {
ArrayList<Object> list = new ArrayList<Object>();
RecordModel record = new RecordModel();
try {
String time = "";
if (object.getLong("time") != 0) {
time = object.getLong("time") + "";
} else {
time = new Date().getTime() + "";
}
record.setAdd_time(time);
record.setCcode(object.getString("country_code"));
record.setCid(object.getString("cell_id"));
record.setGid("9");
record.setJclb(object.getString("yys"));// 保存运营商字段值
record.setLac(object.getString("lac_code"));
record.setLat(object.getDouble("lat") + "");
record.setLon(object.getDouble("lon") + "");
record.setNcode(object.getString("ncode"));
record.setRaduis(object.getString("radius"));
record.setWzbs(object.getString("loc_method"));// 保存位置获取方式
record.setXhqd(object.getString("signal_strength"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(record);
Optdb_interfce db = new Optdb_interfce(context);
db.updateToDb("T_RECORDS", list);
db.close_SqlDb();
// Toast.makeText(context, "保存成功",
// Toast.LENGTH_SHORT).show();
}
}