发送短信发送位置?

时间:2015-04-14 19:21:15

标签: java android geolocation sms google-play-services

快速提问,我已经安排了课程设置并发送了短信,而且我没有错误。我正在尝试将我的位置作为短信的一部分发送为Google地图链接。问题是我的邮件正在发送但位置链接没有通过?但是,该文件没有错误,并且消息传递正常。

有什么想法吗?我还包括"访问好位置"在我的清单文件中。

类别:

Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
String message;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_sms);
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
    txtMessage = (EditText) findViewById(R.id.txtMessage);


    btnSendSMS.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            String phoneNo = txtPhoneNo.getText().toString();
            String message = txtMessage.getText().toString();
            displayLocation();

            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();
        }
    });
}


    private void displayLocation(){
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new LocationListener(){
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {}
            @Override
            public void onProviderEnabled(String s) {}
            @Override
            public void onProviderDisabled(String s) {}
            @Override
            public void onLocationChanged(final Location location) {}
        });
        Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        double longitude = myLocation.getLongitude();
        double latitude = myLocation.getLatitude();
        message+="https://www.google.co.id/maps/@"+latitude+","+longitude;
}





//---sends a SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{

    PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, Home.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 android.telephony.gsm.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case android.telephony.gsm.SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case android.telephony.gsm.SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case android.telephony.gsm.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));

    android.telephony.gsm.SmsManager smms = android.telephony.gsm.SmsManager.getDefault();
    smms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

1 个答案:

答案 0 :(得分:0)

message中设置的displayLocation()变量是您活动的成员变量(在代码顶部声明)。所以当你这样做时:

String message = txtMessage.getText().toString();
displayLocation();

未修改本地变量message

您可以做的是从displayLocation返回字符串并将其添加到消息中:

private String displayLocation() {
    //...
    return "https://www.google.co.id/maps/@"+latitude+","+longitude;
}

String message = txtMessage.getText().toString() + displayLocation();
if (phoneNo.length()>0 && message.length()>0)
//...

我相信你应该删除message类成员变量,如果你没有在其他任何地方使用它。