【问题标题】:How should I send my GPS coordinates by SMS?我应该如何通过短信发送我的 GPS 坐标?
【发布时间】:2012-06-30 17:49:49
【问题描述】:

我在使用 SmsManager 通过 SMS 发送坐标时遇到问题。

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

ZoltrixGPSActivity 类

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 类

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>

【问题讨论】:

    标签: android android-intent gps sms bundle


    【解决方案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>
    

    并在 manifast 中添加这些权限

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

    【讨论】:

    • Thx,但我在 Manifest.xml 中仍然有问题,我认为在 Activity 中。安装后我有 2 个图标。
    • 然后将 ZoltrixGPSActivity 声明为
    • 当我进行更改并单击“发送”时,应用程序崩溃:(
    • @Zoltrix : 如果您在应用程序崩溃时发布 logcat 结果,那么我们只需 1 秒即可解决您的问题
    • 再次感谢您的帮助,几乎一切正常,但屏幕上不显示数据(只是在前面闪过),但一切都在短信中。
    【解决方案2】:

    您将 double & float 并作为字符串从意图尝试使两者都相同

    尝试一次这种方式..

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

    【讨论】:

    • 你不应该启动活动
    • 一般是的[屏幕上不显示数据(只是前面闪过)],谢谢大家的帮助
    • 几乎一切正常,只有信息不显示在屏幕上(开始时只闪烁一次)。使用 SMS 一切正常。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多