我该如何通过短信发送我的GPS坐标?

时间:2012-06-30 17:49:50

标签: android android-intent gps sms bundle

使用SmsManager通过短信发送坐标时遇到问题。

我的类之间的传输变量有问题,所以我使用了Intent和Bundle,但它只显示 NULL 。 这种方法有错吗? 我应该使用数据库吗?请帮忙吗?

ZoltrixGPSActivity Class

package com.zoltrix.gps;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ZoltrixGPSActivity extends Activity {

    TextView textLat;
    TextView textLong;
    TextView textAlt;
    TextView textPro;
    TextView textAcc;
    TextView textSpeed;
    public String onLocat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 = (Button) findViewById(R.id.buttonExit);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // exit
                finish();
                System.exit(0);
            }
        });

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);
        textAlt = (TextView) findViewById(R.id.textAlt);
        textPro = (TextView) findViewById(R.id.textPro);
        textAcc = (TextView) findViewById(R.id.textAcc);
        textSpeed = (TextView) findViewById(R.id.textSpeed);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }

    class mylocationlistener implements LocationListener {

        public void onLocationChanged(Location location) {
            if (location != null) {
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();
                double pAlt = location.getAltitude();
                String PPro = location.getProvider();
                float PAcc = location.getAccuracy();
                float PSpeed = location.getSpeed();

                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));
                textAlt.setText(Double.toString(pAlt));
                textPro.setText(PPro);
                textAcc.setText(Float.toString(PAcc));
                textSpeed.setText(Double.toString(PSpeed));

                Intent i = new Intent(ZoltrixGPSActivity.this,
                        SendSMSActivity.class);
                i.putExtra("wysokosc", pLat);
                i.putExtra("speed", PSpeed);
                startActivity(i);

            }

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

}

SendSMSActivity Class

package com.zoltrix.gps;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;

public class SendSMSActivity extends Activity {
    Button btnSendSMS;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        btnSendSMS.setOnClickListener(new View.OnClickListener()

        {

            public void onClick(View v) {

                Bundle extras = getIntent().getExtras();

                String ALT = extras.getString("wysokosc");
                String SPE = extras.getString("speed");

                sendSMS("510100100", "wys" + " " + ALT + "   " + "spee" + " - "
                        + SPE);
            }

        });
    }

    // ---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message) {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zoltrix.gps"
    android:versionCode="1"
    android:versionName="1.2" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="8" />

    <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.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.zoltrix.gps.SendSMSActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.zoltrix.gps.ZoltrixGPSActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

你正在加倍和放弃浮动并从意图中取出字符串尝试使两者相同

尝试这种方式..

i.putExtra("wysokosc", pLat+"");
i.putExtra("speed", PSpeed+"");

答案 1 :(得分:1)

尝试:

i.putExtra("wysokosc",Double.toString(pLat));
i.putExtra("speed", Float.toString(PSpeed));

而不是

i.putExtra("wysokosc", pLat);
i.putExtra("speed", PSpeed);

并在manifast.xml中声明ZoltrixGPSActivity:

<activity
            android:name=".ZoltrixGPSActivity" />

而不是

<activity
            android:name="com.zoltrix.gps.ZoltrixGPSActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

并在showast中添加这些权限

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>