Techrens Alert – part 5

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;

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(;

        message = (TextView) findViewById(;
        number = (EditText) findViewById(;

        FloatingActionButton fab = (FloatingActionButton) findViewById(;
        fab.setOnClickListener(new View.OnClickListener() {
            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(;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

        final Button openLink = (Button) findViewById(;
        openLink.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (location != null) {




    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;
            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 + " " + "" + location.getLatitude() + "," + location.getLongitude(), null, null);

            Toast.makeText(getBaseContext(), addressText, Toast.LENGTH_LONG).show();
        catch (IOException e) {



    private void getUrl(){

        String myUrl = "" + location.getLatitude() + "," + location.getLongitude();

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myUrl));


    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) {
            location = locationManager.getLastKnownLocation(mprovider);
            locationManager.requestLocationUpdates(mprovider, 15000, 1, this);

            if (location != null)
                Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show();

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    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 == {
            return true;

        return super.onOptionsItemSelected(item);

    public void onLocationChanged(Location location) {

        message.setText("Current Longitude:" + location.getLongitude() +  "\n\nCurrent Latitude:" + location.getLatitude());


    public void onStatusChanged(String s, int i, Bundle bundle) {


    public void onProviderEnabled(String s) {


    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 😉



Leave a Reply

Your email address will not be published. Required fields are marked *