I want that user presses button, after pressing button gps should fetch coordinates automatically in background, as you know gps may take some minutes to fetch coordinates, i just want thet gps take its time and after fetching coordinates then execute a method named 'SendLocationSms',
if i use onLocationChanged method in doInBackground it gives an error, or if i fetch coordinates without using async task, then i have to wait for coordinates, and after fetching i can press the button to execute method SendLocationSms,
i want that user just have to press a button while gps is on, then gps take time as much it needed to fetch coordinates, after fetching then automatically run my desired method without noticing the user.
Basicly i am developing an app which send user's location via sms on pressing alert button, user just have to turn on gps and press alert button nd app should do the rest, i dont want that user wait till gps to fetch coordinates to send alert.
please help me
public class MainActivity extends AppCompatActivity implements LocationListener {
private LocationManager n;
private double lat;
private double lot;
private Button save;
private TextView textSMS;
private String gps;
private String sms;
public Button con;
DatabaseHelper myDb;
String id, cell1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con = (Button) findViewById(R.id.con);
con.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Contacts.class);
startActivity(i);
}
});
myDb = new DatabaseHelper(this);
Cursor c = myDb.getAllData();
c.moveToFirst();
cell1 = (c.getString(1));
save = (Button) findViewById(R.id.save);
textSMS = (TextView) findViewById(R.id.editTextSMS);
textSMS.setText("Help me please im at: [location]");
sms="Help me please im at: ";
n = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
n.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 100, this);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( gps != null ) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(cell1.toString(), null, sms + gps, null, null);
Toast.makeText(getApplicationContext(), "Your message sent successfuly !",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failddddd!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
@Override
public void onLocationChanged(final Location l) {
this.lat=l.getLatitude();
this.lot=l.getLongitude();
this.gps = "\n" +
"www.google.com/maps/place/"+lat+","+lot;
TextView locc = (TextView) findViewById(R.id.locc);
locc.setTextColor(Color.GREEN);
locc.setText("New Location Fetched !");
Toast.makeText(getApplicationContext(), "Location done", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(),"Gps enabled",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Toast.makeText(getBaseContext(), "Gps is turned off!! ",
Toast.LENGTH_SHORT).show();
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
listen, on clicking the button from activity, i want to send longitude and latitude by sms, i turn on gps, and press button, but u know gps takes a minute or two to fetch coordinates, so if i press button before getting coordinates ofcourse it will send null as latitude and longitude, so i want that if still gps not got coordinates but user pressed the button, then app shoild wait until location fetched and then send sms containing of latitude and longitude. so this is detail :) hope now u can help me , thanks alot
In your activity you need to record the current location.
You can now use this variable in your asyncTask when the user will click on send location via sms.
And don't forget somewhere in your code to request location updates as an example look at this:
EDIT: You can the following. Add a boolean that notify that the user want to send current location and when the location is ready, you can send it. So you will have to modify your onclick listener and the location update listener in this way: