【问题标题】:Sending Location with SMS Message Issue?发送带有 SMS 消息问题的位置?
【发布时间】:2015-04-14 19:21:14
【问题描述】:

快速提问,我已经完成了课程设置并发送了 SMS 消息,并且没有任何错误。我正在尝试将我的位置作为 SMS 消息的一部分作为谷歌地图链接发送。问题是我的消息正在发送但位置链接没有通过?但是,该文件没有错误,并且消息传递正常。

有什么想法吗?我还在我的 Manifest 文件中包含了“访问最佳位置”。

类:

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

【问题讨论】:

  • 有什么问题??您实际上没有向我们提供有关问题所在的详细信息。
  • @tnw 我的错误只是编辑了那个。基本上我的消息传递工作但谷歌位置链接没有通过我的消息但没有错误?并且该应用程序有效...有什么想法吗?

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


【解决方案1】:

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

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 类成员变量。

【讨论】:

  • ...抱歉有点不确定该怎么做你能解释一下我如何修改变量以使其工作吗?这一切也有点新意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2021-04-29
  • 1970-01-01
相关资源
最近更新 更多