2012-05-28 48 views
-1

我想做一個android應用程序,它可以檢索位置,發送短信給Ozeki NG服務器。發送消息之後,可以保存到MySQL並從PHP中查看的消息的詳細信息。 任何人都可以教我如何將android sms應用程序連接到MySQL和PHP?如何從Android應用程序連接到PHP和MySql?

我已經測試過。它可以檢索經度和緯度,但谷歌地圖din會出現。它是我的編碼有問題嗎?這是我第一次來創建Android應用程序,我非常感謝從所有的周到,謝謝

得到幫助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> 
+0

你需要做的請求(POST最好)的Web服務(你的PHP與MySQL的一個)的東西給你。不要忘記添加一些數據來請求。例如,你需要像這樣發佈數據到URL:http://sms.com/send/ – Andreyco

+0

對不起,我仍然困惑。你能列出我應該做什麼嗎?我明白需要請求我的PHP和MySQL,但eclipse也需要添加一些編碼來鏈接它的權利? – Rayminn

回答

0

最好的建議是查找或者是REST或SOPA API。使用PHP設置訪問您的MySQL,並在Java中查找如何發送HTTP請求。

阿卡,是這樣的:How to send HTTP request in java?

+0

我的PHP可以連接到MySQL d,但我不知道如何在發送消息後與我的android連接。 – Rayminn

+0

你不想在應用程序中分發你的MySQL憑據。相反,使用PHP作爲中間人,所以您使用PHP進行身份驗證,然後PHP可以使用MySQL服務器進行身份驗證。 – Ben

+0

'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; ((inputLine = in.readLine())!= null) System.out.println(inputLine); in.close(); } }' – Rayminn

相關問題