I wanna send location via sms from Broadcast Receiver. The code doesn't work at all in determining my location. Anyone can help me to solve my problem?
SmsRemoteController
public class SmsRemoteController extends BroadcastReceiver {
private static final int MODE_WORLD_READABLE = 1;
private String smsFirstCode;
private SharedPreferences myPrefs;
private Context contexts;
private String sendingNumber = "";
@Override
public void onReceive(Context context, Intent intent) {
contexts = context;
myPrefs = context.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String smsMode = myPrefs.getString("state", "not");
AppLocationService appLocationService;
appLocationService = new AppLocationService(SmsRemoteController.class);
if (smsMode.equals("ON")) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String smsBody = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sendingNumber += msgs[i].getOriginatingAddress();
smsBody = msgs[i].getMessageBody().toString();
}
// Toast.makeText(contexts, "number"+sendingNumber+"..body"+smsBody.toLowerCase(), Toast.LENGTH_SHORT).show();
if (smsBody.equals("locate")) {
Location nwLocation = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation != null) {
double latitude = nwLocation.getLatitude();
double longitude = nwLocation.getLongitude();
Toast.makeText(contexts, "Mobile Location (NW): \nLatitude: " + latitude + "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
} else {
Location gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
Toast.makeText(contexts, "Mobile Location (GPS): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
}
}
//abortBroadcast();
}// end of onReceive()
}
} // end of the class
}
AppLocationService
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}