Techrens Alert – part 5
We are making good progress, so for this step will will send a text message with the information we have.
Part 5
First we need to add the following the the manifest:
<uses-permission android:name=”android.permission.SEND_SMS” />
<uses-permission android:name=”android.permission.READ_PHONE_STATE” />
Next I added at edit text number field to the content_text1.xml and named the ID txtNumber.
After that I added the following code (look for blue highlights)
public class TestActivity1 extends AppCompatActivity implements LocationListener { LocationManager locationManager; String mprovider; Location location; TextView message; EditText number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test1); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); message = (TextView) findViewById(R.id.txtMessage); number = (EditText) findViewById(R.id.txtNumber); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); final Button button = (Button) findViewById(R.id.btnCheckGps); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { getLocation(); } }); final Button openLink = (Button) findViewById(R.id.btnOpenLink); openLink.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (location != null) { getGeoInfo(); getUrl(); } // } }); } private void getGeoInfo(){ Geocoder geocoder; List<Address> addresses; geocoder = new Geocoder(this, Locale.getDefault()); try { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5 String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() String city = addresses.get(0).getLocality(); String state = addresses.get(0).getAdminArea(); String country = addresses.get(0).getCountryName(); String postalCode = addresses.get(0).getPostalCode(); String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL String addressText = address + " " + city + ", " + state; message.setText(addressText); String iNumber = number.getText().toString(); if (iNumber != null && !iNumber.isEmpty() && !iNumber.equals("null")) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(number.getText().toString(), null, "I'm at " + addressText + " " + "maps.google.com/maps?q=" + location.getLatitude() + "," + location.getLongitude(), null, null); } Toast.makeText(getBaseContext(), addressText, Toast.LENGTH_LONG).show(); } catch (IOException e) { } } private void getUrl(){ String myUrl = "http://maps.google.com/maps?q=" + location.getLatitude() + "," + location.getLongitude(); Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myUrl)); startActivity(browserIntent); } private void getLocation() { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); mprovider = locationManager.getBestProvider(criteria, false); if (mprovider != null && !mprovider.equals("")) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } location = locationManager.getLastKnownLocation(mprovider); locationManager.requestLocationUpdates(mprovider, 15000, 1, this); if (location != null) onLocationChanged(location); else Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_test_activity1, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onLocationChanged(Location location) { message.setText("Current Longitude:" + location.getLongitude() + "\n\nCurrent Latitude:" + location.getLatitude()); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }
I defined the edit text number box and point it txtNumber. Once done I added code to the getGeoInfo method. I then get the value that is in the textNumber and assign it to the iNumber variable. After that I do a check to make sure there is some value in the text field. If there is a value then we use SmsManager to send the text. The SmsManager takes the number and the message and then sends the text message.
And there you have it you can now send a text message with your location to a number of your choosing.
So here is the apk to test with:
I would love to hear what you think so far 🙂 so please leave some comments below 😉