我遇到了问题,因为无论何时运行此代码,它都会使用“Check In Success”消息来发送我的手机消息我只希望它发送一次,当位置坐标与上一次知识相同或几乎相同时坐标。
以下是代码:
public class SmsReceiver extends BroadcastReceiver {
private static final String TAG = "Message recieved";
@Override
public void onReceive(Context context, Intent intent) {
if(isMyServiceRunning(context)) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
LocationManager lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// Geocoder geocoder;
String bestProvider;
// List<Address> user = null;
double lat;
double lng;
Criteria criteria = new Criteria();
bestProvider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bestProvider);
if(location != location)
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("09164232662", null, "You Have Successfully reached your target Location" , null, null);
}
/*else
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("09164232662", null, "Please Walk a few more meters in order to reach the designated location" , null, null);
}*/
/*Toast.makeText(
context,
"FROM: " + messages.getOriginatingAddress() + "time: "
+ messages.getTimestampMillis() + "SMS Received : "
+ messages.getMessageBody(), Toast.LENGTH_LONG).show();*/
String date=String.valueOf(messages.getTimestampMillis());
HashMap<String, String> data = new HashMap<String, String>();
data.put("phone", messages.getOriginatingAddress());
data.put("text", messages.getMessageBody());
//data.put("quando", date);
if(messages.getOriginatingAddress() == "09228387118")
{
abortBroadcast();
}
//AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data);
//asyncHttpPost.execute("http://10.0.0.2/smsreceive.php",messages.getOriginatingAddress());
//abortBroadcast();
}else{
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
LocationManager lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// Geocoder geocoder;
String bestProvider;
// List<Address> user = null;
double lat;
double lng;
Criteria criteria = new Criteria();
bestProvider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bestProvider);
/* {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("09164232662", null, "Please Walk a few more meters in order to reach the designated location" , null, null);
}*/
String date=String.valueOf(messages.getTimestampMillis());
HashMap<String, String> data = new HashMap<String, String>();
data.put("phone", messages.getOriginatingAddress());
data.put("text", messages.getMessageBody());
//data.put("quando", date);
data.put("servicestatus", "1");
//09228387118
if(messages.getOriginatingAddress() == "09164232662")
{
abortBroadcast();
}
//09989988910
//AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data);
//asyncHttpPost.execute("http://10.0.0.2/smsreceive.php",messages.getOriginatingAddress());
//abortBroadcast();
}
}
boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (ServerService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}