-1

I want to do an android app which can retrieve location, send sms to Ozeki NG server. After send the message, the details of the message that can save into MySQL and view from PHP. Anyone can teach me how to connect android sms app to MySQL and PHP?

I test already. It can retrieve the Longitude and Latitude, but the google map din appear. Is it my coding have problem? This is my first time to create android app, I'm greatly appreciate to get help from all of you.Thanks

ParkingPaymentLBSMapsActivity.java

package com.android.googlemap;

import com.google.android.maps.GeoPoint
import com.google.android.maps.GeoPoint;`
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ParkingPaymentLBSMapsActivity extends MapActivity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;



MapController mControl;
GeoPoint GeoP;
MapView mapV;
MyLocationOverlay compass;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapV = (MapView) findViewById(R.id.mapView);
    mapV.displayZoomControls(true);
    mapV.setBuiltInZoomControls(true);

    double lat = 40.8;
    double longi = -96.666;

    GeoP = new GeoPoint ((int) (lat *1E6), (int) (longi *1E6) );

    mControl = mapV.getController();        
    mControl.animateTo(GeoP);
    mControl.setZoom(13);

    compass = new MyLocationOverlay(this, mapV);
    mapV.getOverlays().add(compass);

    Button next = (Button) findViewById(R.id.nextbtn);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), SMS.class);
            startActivityForResult(myIntent, 0);
        }

       });

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
            );


    retrieveLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showCurrentLocation();
        }

    });

}

protected void showCurrentLocation() {
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(ParkingPaymentLBSMapsActivity.this, message,
                Toast.LENGTH_LONG).show();                  
    }
}


private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(ParkingPaymentLBSMapsActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(ParkingPaymentLBSMapsActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(ParkingPaymentLBSMapsActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(ParkingPaymentLBSMapsActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();

}

        }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        compass.disableCompass();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        compass.enableCompass();
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

SMS.java

package com.android.googlemap;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMS extends Activity{

Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sms);        
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
    txtMessage = (EditText) findViewById(R.id.txtMessage);

    Button next = (Button) findViewById(R.id.Button02);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }

    });

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {               
            String phoneNo = txtPhoneNo.getText().toString();
            String message = txtMessage.getText().toString();               
            if (phoneNo.length()>0 && message.length()>0)                
                sendSMS(phoneNo, message);                
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
        }
    });        
}

//---sends a SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{      
    /*
    PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, test.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    */

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                      
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);               
}    

}

AndroidManifest.xml

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

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

<uses-library android:name = "com.google.android.maps" />


    <activity
        android:name=".ParkingPaymentLBSMapsActivity"
        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=".SMS"></activity>

</application>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>

<uses-sdk android:minSdkVersion="10" />

</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.google.android.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="0VV4vaNBXBx4Vu19jim2eoGAn5BnbatPvHRer5Q"
             />


<Button
    android:id="@+id/retrieve_location_button"
    android:text="Retrieve Location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />


<Button
    android:id="@+id/nextbtn"
    android:layout_width="wrap_content"
    android:layout_height="55px"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/retrieve_location_button"
    android:text="Next"
    android:textSize="18px" />

</RelativeLayout>
Rayminn
  • 21
  • 3
  • You need to do request (POST preferably) to the Web service (your PHP&MySQL one) that to the stuff for you. Do not forget to add some data to request. For instance, you need to post data to URL like this: http://sms.com/send/ – Andreyco May 28 '12 at 16:29
  • Sorry, im still confused. Can you list the step what shoud i do? I understand need to request my PHP and MySQL, but the eclipse also need to add some coding to link it right? – Rayminn May 28 '12 at 16:51

1 Answers1

0

Best advice is look up either a REST or SOPA API. Set that up with PHP to access your MySQL, and look up in Java how to send HTTP requests.

Aka, something like: How to send HTTP request in java?

Community
  • 1
  • 1
Ben
  • 8,230
  • 6
  • 45
  • 61
  • my PHP can connect to MySQL d, but i do not know how to connect with my android after send the message. – Rayminn May 28 '12 at 17:00
  • You don't want to distribute your MySQL credentials with your application. Instead use PHP as the middle man, so you authenticate with the PHP, and then PHP can auth with the MySQL server. – Ben May 28 '12 at 17:07
  • `import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL localhost = new URL("http://localhost:8080/MobileParkingPaymentWebpage/"); URLConnection yc = localhost.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }` – Rayminn May 28 '12 at 17:17
  • so is it need to add this URLConnectionReader.java in my eclipse? – Rayminn May 28 '12 at 17:24
  • More or less, then instead of the URL there (localhost/etc) you'd put in the url to your REST framework. Then POST or GET the variables over, and the reply (could be in anything, xml, json, etc) can be parsed by the Java app you're writing – Ben May 28 '12 at 17:56