textview成为Android的一个短信体

时间:2014-02-27 20:42:15

标签: java android sms

我正在为Android制作应用程序,当您打开应用程序时,它会在文本视图中通过GPS显示您的确切坐标。然后按一个按钮,将SMS与您的坐标一起发送到预设号码。我有一切正常,但短信身体。我无法弄清楚如何使用您的坐标获取textview并将其发送到SMS正文中。任何帮助都是极好的!

MAIN JAVA PAGE

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements LocationListener{
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude,longitude; 
    protected boolean gps_enabled,network_enabled;
    Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtLat = (TextView) findViewById(R.id.textview1);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1000, this);




        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            button = (Button) findViewById(R.id.button1);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    try {

                        String messageToSend = "R.id.textview1";
                        String number = "8143664050";

                        SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);


                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "SMS failed!",
                                Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            });
        }

    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }

}

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textview1"
        android:layout_marginRight="16dp"
        android:layout_marginTop="22dp"
        android:onClick="gotoActivity"
        android:text="Button" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试改变这一点:

String messageToSend = "R.id.textview1";

到这个

TextView tv = (TextView) findViewById(R.id.textview1);
String messageToSend = tv.getText().toString();