使用SMS管理器发送带有GPS位置的SMS [英] Send SMS with GPS location using SMS manager

查看:99
本文介绍了使用SMS管理器发送带有GPS位置的SMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取GPS位置并通过Sms与相关消息一起发送的帮助.我已经开发了一个紧急按钮应用程序,该应用程序应该发送自定义消息+ GPS位置.我正在努力实现GPS定位功能.

I need help getting GPS location and sending it via Sms along with the relevant message. I have developed an emergency button app which should send a custom message + GPS location. I am struggling with implementing the GPS location function.

不带GPS的原始代码:/此代码功能不错,但是其中不包含GPS位置/

Original code without GPS: /This code funtcions well however it does not contain the GPS location/

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText customMessage;



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

        customMessage=(EditText)findViewById(R.id.customMessage);

    }

    public void sendText(View view)
    {


        String message = "";
        if (view.getId() == R.id.fab)
        {
            message = customMessage.getText().toString();
        }
        else
        {
            Button sender = (Button)view;
            message = sender.getText().toString();
        }
        //Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("6505551212", null,message + "(Location Here)", null, null);
    }


}

带有GPS的代码:/代码的问题是,当我单击发送按钮时,应用程序只是崩溃了,但没有错误/

Code with GPS: /The problem with the code is that the app just crashes when I click the send button but it has no errors/

import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText customMessage;



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

        customMessage=(EditText)findViewById(R.id.customMessage);

    }

    public void sendText (View view, String phoneNumber, Location currentLocation) {

        String message = "";
        if (view.getId() == R.id.fab)
        {
            message = customMessage.getText().toString();
        }
        else
        {
            Button sender = (Button)view;
            message = sender.getText().toString();
        }


        SmsManager smsManager = SmsManager.getDefault();
        StringBuffer smsBody = new StringBuffer();
        smsBody.append("http://maps.google.com?q=");
        smsBody.append(currentLocation.getLatitude());
        smsBody.append(",");
        smsBody.append(currentLocation.getLongitude());
        smsManager.sendTextMessage("6505551212", null, smsBody.toString(), null, null);
    }
}

推荐答案

您先前的代码可以正常工作,因为其消息长度为< = 160.但是另一则消息包含的URL大于160个字符.为了克服这个问题,您需要分多个部分发送消息.使用下面给出的代码:SmsManager sms = SmsManager.getDefault();ArrayList< String>零件= sms.divideMessage(messageString);sms.sendMultipartTextMessage(number,null,parts,null,null);

Your previous code working because its message length is <=160. But the another message contains url which is larger than 160 characters. To overcome this you need to send message in multiple parts. Use the code given below : SmsManager sms = SmsManager.getDefault(); ArrayList<String> parts = sms.divideMessage(messageString); sms.sendMultipartTextMessage(number, null, parts, null, null);

这篇关于使用SMS管理器发送带有GPS位置的SMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆