2

我在使用 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

        }

    }

}

发送短信活动类

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>
4

2 回答 2

1

你把 double & float 并从意图中作为字符串尝试使两者相同

尝试这种方式一次..

i.putExtra("wysokosc", pLat+"");
i.putExtra("speed", PSpeed+"");
于 2012-06-30T17:52:47.517 回答
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"/>
于 2012-06-30T17:58:29.450 回答