super.ondestory(); 如果(locationManager!=null) locationManager.RemoveUpdate(此); 未注册接收人(proximityAlertReceiver); } @凌驾 公共布尔onUnbind(意图){ //TODO自动生成的方法存根 返回super.onUnbind(intent); } 公共无效sendLongLat(最终字符串lati,最终字符串longi){ 新建异步任务(){ @凌驾 受保护的void onPostExecute(字符串结果){ //TODO自动生成的方法存根 super.onPostExecute(结果); 字符串msg=null; 试一试{ JSONObject json=新的JSONObject(结果); String status=json.getString(“status”); if(状态相等信号情况(“正常”)){ msg=“已发送位置”; }否则{ msg=“出了问题”; } Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show(); }捕获(JSONException e){ //TODO自动生成的捕捉块 e、 printStackTrace(); }捕获(例外e){ //TODO:处理异常 e、 printStackTrace(); } } @凌驾 受保护的字符串doInBackground(字符串…参数){ //TODO自动生成的方法存根 ArrayList行=新的ArrayList(); 连接数据库=新连接(); 字符串routeId=Singelton.getInstance().getRouteId(); if(Utilities.checkInternetConnection(getApplicationContext())){ 试一试{ lines=db.putLatLong(“putLatLong”,“2”//route id ,朗吉,拉蒂); }捕获(例外e){ //TODO自动生成的捕捉块 e、 printStackTrace(); } } 返回行。获取(0); } }.execute(); } 私有void setProximityAlert(LocationModel obj){ locationManager=(locationManager)getSystemService(Context.LOCATION\u服务); 如果(!locationManager.isProviderEnabled(locationManager.GPS\U提供程序)){ AlertDialog.Builder=新建AlertDialog.Builder(上下文); builder.setTitle(“GPS未启用”);//找不到GPS builder.setMessage(“请打开设备的GPS”);//要启用吗? setPositiveButton(“设置”,新的DialogInterface.OnClickListener(){ 公共void onClick(DialogInterface,inti){ context.startActivity(新意图(android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings)); } }); builder.setNegativeButton(“确定”,空); builder.create().show(); 返回; } locationManager.RequestLocationUpdate(locationManager.GPS\U提供程序, 最小更新间隔,最小更新距离,此); 意向意向=新意向(PROX_ALERT_Intent+“+obj.getPickupId())// 意向。额外(“位置”,obj); PendingIntent proximityIntent=PendingIntent.getBroadcast(上下文,Integer.parseInt(obj.getPickupId())//唯一id/请求代码 ,意图,挂起内容。标志(取消当前); locationManager.addProximityAlert( obj.getLat(),//警报区域中心点的纬度 obj.getLongi(),//警报区域中心点的经度 半径,//警报区域中心点的半径,单位为米 PROX_ALERT_EXPIRATION_TIME,//此接近警报的时间,以毫秒为单位,或-1表示无过期 当检测到进入或退出警报区域时,ProximityEvent//将用于生成开火意图 ); 过滤器=新意图过滤器(PROX\u ALERT\u INTENT); } 私有无效注册表接收者() { proximityAlertReceiver=新位置AlertReceiver(); 寄存器接收器(近端接收器、滤波器); } }
和接近警报接收器super.ondestory(); 如果(locationManager!=null) locationManager.RemoveUpdate(此); 未注册接收人(proximityAlertReceiver); } @凌驾 公共布尔onUnbind(意图){ //TODO自动生成的方法存根 返回super.onUnbind(intent); } 公共无效sendLongLat(最终字符串lati,最终字符串longi){ 新建异步任务(){ @凌驾 受保护的void onPostExecute(字符串结果){ //TODO自动生成的方法存根 super.onPostExecute(结果); 字符串msg=null; 试一试{ JSONObject json=新的JSONObject(结果); String status=json.getString(“status”); if(状态相等信号情况(“正常”)){ msg=“已发送位置”; }否则{ msg=“出了问题”; } Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show(); }捕获(JSONException e){ //TODO自动生成的捕捉块 e、 printStackTrace(); }捕获(例外e){ //TODO:处理异常 e、 printStackTrace(); } } @凌驾 受保护的字符串doInBackground(字符串…参数){ //TODO自动生成的方法存根 ArrayList行=新的ArrayList(); 连接数据库=新连接(); 字符串routeId=Singelton.getInstance().getRouteId(); if(Utilities.checkInternetConnection(getApplicationContext())){ 试一试{ lines=db.putLatLong(“putLatLong”,“2”//route id ,朗吉,拉蒂); }捕获(例外e){ //TODO自动生成的捕捉块 e、 printStackTrace(); } } 返回行。获取(0); } }.execute(); } 私有void setProximityAlert(LocationModel obj){ locationManager=(locationManager)getSystemService(Context.LOCATION\u服务); 如果(!locationManager.isProviderEnabled(locationManager.GPS\U提供程序)){ AlertDialog.Builder=新建AlertDialog.Builder(上下文); builder.setTitle(“GPS未启用”);//找不到GPS builder.setMessage(“请打开设备的GPS”);//要启用吗? setPositiveButton(“设置”,新的DialogInterface.OnClickListener(){ 公共void onClick(DialogInterface,inti){ context.startActivity(新意图(android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings)); } }); builder.setNegativeButton(“确定”,空); builder.create().show(); 返回; } locationManager.RequestLocationUpdate(locationManager.GPS\U提供程序, 最小更新间隔,最小更新距离,此); 意向意向=新意向(PROX_ALERT_Intent+“+obj.getPickupId())// 意向。额外(“位置”,obj); PendingIntent proximityIntent=PendingIntent.getBroadcast(上下文,Integer.parseInt(obj.getPickupId())//唯一id/请求代码 ,意图,挂起内容。标志(取消当前); locationManager.addProximityAlert( obj.getLat(),//警报区域中心点的纬度 obj.getLongi(),//警报区域中心点的经度 半径,//警报区域中心点的半径,单位为米 PROX_ALERT_EXPIRATION_TIME,//此接近警报的时间,以毫秒为单位,或-1表示无过期 当检测到进入或退出警报区域时,ProximityEvent//将用于生成开火意图 ); 过滤器=新意图过滤器(PROX\u ALERT\u INTENT); } 私有无效注册表接收者() { proximityAlertReceiver=新位置AlertReceiver(); 寄存器接收器(近端接收器、滤波器); } },android,broadcastreceiver,locationlistener,Android,Broadcastreceiver,Locationlistener,和接近警报接收器 public class LocationAlertReceiver extends BroadcastReceiver{ private int NOTIFICATION_ID=1000; public static String PROX_ALERT_INTENT = "com.ginormous.transportmanagement.ProximityAlert";
public class LocationAlertReceiver extends BroadcastReceiver{
private int NOTIFICATION_ID=1000;
public static String PROX_ALERT_INTENT = "com.ginormous.transportmanagement.ProximityAlert";
private String uniqueid="";
LocationModel locaObj;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub\
this.context=context;
try {
LocationModel model=((LocationModel) intent.getSerializableExtra("LOCATION"));
Toast.makeText(context, "Proxmity alert receiver; id : "+model.getPickupId(),
Toast.LENGTH_SHORT).show();
removeProximityAlert(context, PROX_ALERT_INTENT+"."+model.getPickupId());
Toast.makeText(context, "proxmity removed", 1).show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(context, "exception in Proxmity alert receiver",
Toast.LENGTH_SHORT).show();
}
/*if(intent.getExtras().getSerializable("LOCATION")!=null){
locaObj=(LocationModel) intent.getExtras().getSerializable("LOCATION");
}
String KEY=LocationManager.KEY_PROXIMITY_ENTERING;
Boolean isEntering=intent.getBooleanExtra(KEY, false);
if(isEntering){
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent2 = new Intent(context, MainLogin.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = createNotification();
notification.setLatestEventInfo(context, "Proximity Alert!",
"Location : "+locaObj.getPickupName()+",", pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.d(getClass().getSimpleName(), "entering");
removeProximityAlert(context,locaObj.getPickupId());
//this will send the details to server
if(Utilities.checkInternetConnection(context))
new SendArrivalDetails().execute(locaObj.getPickupId());
else
Utilities.sendArrivalDetails(context, locaObj.getPickupId(),"N");
}
else
Log.d(getClass().getSimpleName(), "exiting");
*/
}
private void removeProximityAlert(Context context,String uniqueid2) {
// TODO Auto-generated method stub
try {
LocationManager locationManager=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Intent intent=new Intent(PROX_ALERT_INTENT+"."+uniqueid2);
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, Integer.parseInt(uniqueid2), intent, 0);
locationManager.removeProximityAlert(pendingIntent);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(context, "error in removing promity alert for "+uniqueid2, Toast.LENGTH_SHORT).show();
}
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher_transport;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
public class SendArrivalDetails extends AsyncTask<String, Void, String> {
String pickupid="";
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<String> lines=new ArrayList<String>();
pickupid=params[0];
try {
Connection db=new Connection();
Calendar cal=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
db.saveArrivalDetails("savepickuptimer", pickupid, Singelton.getInstance().getAttType(),sdf.format(cal.getTime()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lines.get(0);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
JSONObject jsonObject=new JSONObject(result);
String response=jsonObject.getString("STATUS");
if(response.equals("STATUS")){
Utilities.sendArrivalDetails(context, pickupid,"U");
Toast.makeText(context, "proximity arrival details sent", 1).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(context, "error sending proximity arrival details", 1).show();
}
}
}
}
公共类LocationAlertReceiver扩展了BroadcastReceiver{
私有int通知_ID=1000;
公共静态字符串PROX\u ALERT\u INTENT=“com.ginormous.transportmanagement.ProximityAlert”;
私有字符串uniqueid=“”;
位置模型locaObj;
语境;
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根\
this.context=context;
试一试{
LocationModel模型=((LocationModel)intent.getSerializableExtra(“位置”);
Toast.makeText(上下文,“Proxmity警报接收器;id:”+model.getPickupId(),
吐司。长度(短)。show();
移除代理
public class LocationAlertReceiver extends BroadcastReceiver{
private int NOTIFICATION_ID=1000;
public static String PROX_ALERT_INTENT = "com.ginormous.transportmanagement.ProximityAlert";
private String uniqueid="";
LocationModel locaObj;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub\
this.context=context;
try {
LocationModel model=((LocationModel) intent.getSerializableExtra("LOCATION"));
Toast.makeText(context, "Proxmity alert receiver; id : "+model.getPickupId(),
Toast.LENGTH_SHORT).show();
removeProximityAlert(context, PROX_ALERT_INTENT+"."+model.getPickupId());
Toast.makeText(context, "proxmity removed", 1).show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(context, "exception in Proxmity alert receiver",
Toast.LENGTH_SHORT).show();
}
/*if(intent.getExtras().getSerializable("LOCATION")!=null){
locaObj=(LocationModel) intent.getExtras().getSerializable("LOCATION");
}
String KEY=LocationManager.KEY_PROXIMITY_ENTERING;
Boolean isEntering=intent.getBooleanExtra(KEY, false);
if(isEntering){
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent2 = new Intent(context, MainLogin.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = createNotification();
notification.setLatestEventInfo(context, "Proximity Alert!",
"Location : "+locaObj.getPickupName()+",", pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.d(getClass().getSimpleName(), "entering");
removeProximityAlert(context,locaObj.getPickupId());
//this will send the details to server
if(Utilities.checkInternetConnection(context))
new SendArrivalDetails().execute(locaObj.getPickupId());
else
Utilities.sendArrivalDetails(context, locaObj.getPickupId(),"N");
}
else
Log.d(getClass().getSimpleName(), "exiting");
*/
}
private void removeProximityAlert(Context context,String uniqueid2) {
// TODO Auto-generated method stub
try {
LocationManager locationManager=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Intent intent=new Intent(PROX_ALERT_INTENT+"."+uniqueid2);
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, Integer.parseInt(uniqueid2), intent, 0);
locationManager.removeProximityAlert(pendingIntent);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(context, "error in removing promity alert for "+uniqueid2, Toast.LENGTH_SHORT).show();
}
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher_transport;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
public class SendArrivalDetails extends AsyncTask<String, Void, String> {
String pickupid="";
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<String> lines=new ArrayList<String>();
pickupid=params[0];
try {
Connection db=new Connection();
Calendar cal=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
db.saveArrivalDetails("savepickuptimer", pickupid, Singelton.getInstance().getAttType(),sdf.format(cal.getTime()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lines.get(0);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
JSONObject jsonObject=new JSONObject(result);
String response=jsonObject.getString("STATUS");
if(response.equals("STATUS")){
Utilities.sendArrivalDetails(context, pickupid,"U");
Toast.makeText(context, "proximity arrival details sent", 1).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(context, "error sending proximity arrival details", 1).show();
}
}
}
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Intent intent = new Intent();
intent.setAction(AppConstants.BROAD_ACTION);
for(int i=0;i<countRegionIDArr.size();i++){
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlertService.this, countRegionIDArr.get(i), intent, 0);
mlocManager.removeProximityAlert(pendingIntent);
}