Sending location via text

I'm Having a problem with sending my location coordinates to my cellphone.

Here is the code of Main.Java

    package com.example.finder;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.telephony.SmsManager;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class Main extends Activity {

TextView textLat;
TextView textLong;
TextView textAlt;

public String onLocat;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn1 = (Button) findViewById(R.id.checkin);
    btn1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Bundle extras = getIntent().getExtras();

            String ALT = extras.getString("wysokosc");
            String SPE = extras.getString("speed");

            sendSMS("09164232662", "wys" + " " + ALT + "   " + "spee" + " - "
                    + SPE);
        }


    });

    textLat = (TextView) findViewById(R.id.textLat);
    textLong = (TextView) findViewById(R.id.textLong);
    textAlt = (TextView) findViewById(R.id.textAlt);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

class mylocationlistener implements LocationListener {

    public void onLocationChanged(Location location) {
        if (location != null) {
            double pLong = location.getLongitude();
            double pLat = location.getLatitude();
            double pAlt = location.getAltitude();
            String PPro = location.getProvider();

            textLat.setText(Double.toString(pLat));
            textLong.setText(Double.toString(pLong));
            textAlt.setText(Double.toString(pAlt));


            Intent i = new Intent(Main.this,
                    Main.class);
            i.putExtra("wysokosc",Double.toString(pLat));
            i.putExtra("speed",Double.toString(pLong));
            startActivity(i);

        }

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }


}

private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage("09164232662", null, message, null, null);
}

}

Here is the code of GPS.Java

package com.example.finder;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;

public class GPS extends Activity {
Button checkout;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);
    checkout = (Button) findViewById(R.id.checkout);
    checkout.setOnClickListener(new View.OnClickListener()

    {

        public void onClick(View v) {

            Bundle extras = getIntent().getExtras();

            String ALT = extras.getString("wysokosc");
            String SPE = extras.getString("speed");

            sendSMS("09164232662", "wys" + " " + ALT + "   " + "spee" + " - "
                    + SPE);
        }

    });
}

// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

}

Here is the code of Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity
        android:name="com.example.finder.GPS"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Here's the logcat

02-02 14:27:03.619: D/libEGL(10290): loaded /system/lib/egl/libEGL_mali.so
02-02 14:27:03.629: D/libEGL(10290): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-02 14:27:03.629: D/libEGL(10290): loaded /system/lib/egl/libGLESv2_mali.so
02-02 14:27:03.634: D/(10290): Device driver API match
02-02 14:27:03.634: D/(10290): Device driver API version: 10
02-02 14:27:03.634: D/(10290): User space API version: 10 
02-02 14:27:03.634: D/(10290): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16     15:37:13 KST 2012 
02-02 14:27:03.659: D/OpenGLRenderer(10290): Enabling debug mode 0
02-02 14:27:05.309: D/AndroidRuntime(10290): Shutting down VM
02-02 14:27:05.309: W/dalvikvm(10290): threadid=1: thread exiting with uncaught exception (group=0x411b72a0)
02-02 14:27:05.314: E/AndroidRuntime(10290): FATAL EXCEPTION: main
02-02 14:27:05.314: E/AndroidRuntime(10290): java.lang.NullPointerException
02-02 14:27:05.314: E/AndroidRuntime(10290):    at     com.example.finder.Main$1.onClick(Main.java:36)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at android.view.View.performClick(View.java:4211)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at android.view.View$PerformClick.run(View.java:17267)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at android.os.Handler.handleCallback(Handler.java:615)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at     android.os.Handler.dispatchMessage(Handler.java:92)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at android.os.Looper.loop(Looper.java:137)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at java.lang.reflect.Method.invoke(Method.java:511)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-02 14:27:05.314: E/AndroidRuntime(10290):    at dalvik.system.NativeStart.main(Native Method)
02-02 14:27:16.474: I/Process(10290): Sending signal. PID: 10290 SIG: 9

The problem is it's getting the signal yet it does not send through my cellphone.

Let me know what wrong I am doing.

Answers


Change to:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    final String ALT = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude();
    final String SPE = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude();

    Button btn1 = (Button) findViewById(R.id.checkin);


    btn1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            sendSMS("09164232662", "wys" + " " + ALT + "   " + "spee" + " - "
                    + SPE);
        }


    });

    textLat = (TextView) findViewById(R.id.textLat);
    textLong = (TextView) findViewById(R.id.textLong);
    textAlt = (TextView) findViewById(R.id.textAlt);


}

I think you have a conceptual problem in your application. First, you have defined two entry points in your manifest, it should be something like:

<activity
        android:name=".Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity android:name="com.example.finder.GPS" />

And then your CPS is quite useless, where you call it?

And whay you call the same activity?

Intent i = new Intent(Main.this, Main.class); i.putExtra("wysokosc",Double.toString(pLat)); i.putExtra("speed",Double.toString(pLong)); startActivity(i); Just define pLong, pLat and pAlt as global and use them inside your onClick to send the sms.

Hope this helps.


Need Your Help

Making table rows clickable on all pages of Jquery DataTables

javascript jquery-plugins twitter-bootstrap datatables

So I've got an implementation of dataTables plugin with pagination. I want a bootstrap modal to appear when I click on any row in the table. So far I can get the modal working when I click rows on ...

Smart client Winform vs MVVM + Prism

wpf winforms client prism smartclient

Is Windows forms Simart Client is a legacy thing now? And is it to be replaced by Prism (and/or MVVM)? Should I bother to learn about Smart Client?