0

(ANDROID GPS)我做了这个应用程序通过短信发送gps坐标,但它似乎没有做任何事情有人能告诉我我错过了什么吗?这就是 logcat 在按下按钮时必须说的

日志猫 图像1 日志猫图像2

MainActivity.java

package com.adzz.gps;

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


public class MainActivity extends Activity { 
    String m; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        LocationManager manager= (LocationManager) 
                this.getSystemService(Context.LOCATION_SERVICE); 
        LocationListener listener =new LocationListener() { 
            @Override 

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

            } 

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

            } 

            @Override 
            public void onProviderDisabled(String provider) { 
            } 
            @Override 
            public void onLocationChanged(Location location) { 
                final String phoneNumber="9453603045"; 

                double lat1=location.getLatitude(); 
                Double d1= new Double(lat1); 
                double longi1=location.getLongitude(); 
                Double d2=new Double(longi1); 
                m="latitude = "+ d1.toString() + "and latitude = "+ d2.toString(); 
                SmsManager smsManager = SmsManager.getDefault(); 
                smsManager.sendTextMessage(phoneNumber, null, m, null, null); 
                TextView lat= (TextView) findViewById(R.id.lat); 
                TextView longi=(TextView) findViewById(R.id.longi); 

                lat.setText("latitude = "+lat1); 
                longi.setText("longitude ="+longi1); 
            } 
        }; 
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 25, 
                listener); 
        Button b= (Button) findViewById(R.id.button1); 
        b.setOnClickListener(new OnClickListener() { 

            @Override 
            public void onClick(View v) { 

                Toast toast=Toast.makeText(Main.this, m, 5000); 
                toast.setGravity(Gravity.CENTER, 0, 0); 
                toast.show(); 
            } 
        }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/longi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/button1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

AndroidManifest.xml

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

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


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.adzz.gps001.MainActivity"
            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

1 回答 1

0

如何在您的 中添加 SMS 权限AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />
于 2014-03-30T17:26:38.787 回答