开发一个徒步旅行应用程序,只需点击应用程序内的按钮,即可发送人的纬度和经度坐标。当我触发发送按钮时,消息仅发送“发送紧急服务到:”nullnull“意味着它不包含消息的 lat 和 long 的值。我有完整的 gps 信号并在清单中具有以下权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.TELEPHONY_MANAGER" />
<uses-permission android:name="android.permission.SEND_SMS" />
我需要在以下代码中进行哪些更改才能使应用程序成功发送带有文本消息的经纬度坐标。为所有“Toast”消息道歉,将它们用于测试目的!
package com.gpscamera;
import java.util.ArrayList;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.telephony.SmsManager;
public class MainActivity extends Activity {
Button sendButton;
ImageView iv; /* result of the data returned in the if statement is
INCLUDED IN THE USER INTERFACE*/
TextView textLat;
TextView textLong;
String long1;
String lat1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLat = (TextView)findViewById(R.id.textLat);
textLong = (TextView)findViewById(R.id.textLong);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
iv=(ImageView) findViewById(R.id.imageView);
/* Reference to out imageview view */
Button btn = (Button) findViewById(R.id.takePhoto);
/* Makes reference to button to start the application, button name is takePhoto */
btn.setOnClickListener(new OnClickListener() {
/* Makes the onclick listener for the button, */
@Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
/* in this onclikc, must define what happens when the button is clicked. */
/* Specified to open the image caputre*/
startActivityForResult(intent, 0);
/* Starts that activity "intent" above */
}
});
Button btn1 = (Button) findViewById(R.id.Send);
btn1.setOnClickListener(new OnClickListener() {
/* Makes the onclick listener for the button, */
@Override
public void onClick(View v) {
/* Starts that activity "intent" above */
Toast.makeText( getApplicationContext(), "eeeeeeeee", Toast.LENGTH_SHORT ).show();
String phoneNumber = "07835588000";
String message = "Send Emergency Services to:";
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
Toast.makeText( getApplicationContext(), "after sms", Toast.LENGTH_SHORT ).show();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
/* When the button is clicked it is going to start the camera, returning a result which is an image
* with the request code from the intent above. */
{
if(requestCode ==0)
/* If the request code is = to 0 we need to get the bitmap image which is returned as out data. */
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
Toast.makeText( getApplicationContext(), "photo saved", Toast.LENGTH_SHORT ).show();
Toast.makeText( getApplicationContext(), "Getting GPS loc", Toast.LENGTH_SHORT ).show();
}
}
class mylocationlistener implements LocationListener{
@Override
public void onLocationChanged(Location Location) {
try {
if(Location != null)
{
double plong = Location.getLongitude();
double plat = Location.getLatitude();
textLat.setText(Double.toString(plat));
textLong.setText(Double.toString(plong));
textLat.append(lat1);
textLong.append(long1);
}
} catch (Exception ex) {
Toast.makeText( getApplicationContext(), "loss signal", Toast.LENGTH_SHORT ).show();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
}