以短信形式发送当前位置 Android Studio

Sending current location as SMS Android Studio

我是 Android Studio 的新手。

我尝试在单击按钮时将我的当前位置作为文本消息发送,但单击时没有任何反应。

首先,这是我的 XML:

<Button android:id="@+id/emergency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:background="@drawable/button_selector1"
        android:drawableEnd="@android:drawable/ic_dialog_alert"
        android:gravity="center"
        android:maxLines="1"
        android:paddingLeft="15dip"
        android:paddingRight="15dip" />

然后,这是我的 MainActivity 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.ly_home);

    emergency = (Button) findViewById(R.id.emergency);

    emergency.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            GPStracker g = new GPStracker(getApplicationContext());
            Location l = g.getLocation();
            if (l != null){
                double lat = l.getLatitude();
                double lon = l.getLongitude();
                String message="http://maps.google.com/maps?saddr="+lat+","+lon;
                String number = "xxxxxxxx";
                SmsManager smsManager = SmsManager.getDefault();
                StringBuffer smsBody = new StringBuffer();
                smsBody.append(Uri.parse(message));
                android.telephony.SmsManager.getDefault().sendTextMessage(number, null, smsBody.toString(), null,null);
            }
        }
    });
}

还有,我的 GPStracker class:

public class GPStracker implements LocationListener{

    Context context;
    public GPStracker(Context c){
        context = c;
    }

    public Location getLocation(){

        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (isGPSEnabled){
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,6000,10,this);
            Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            return l;
        }else{
            Toast.makeText(context,"Please enable GPS", Toast.LENGTH_LONG).show();
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

我在manifest里给了权限:精细定位,粗略定位,收发短信。我做错了什么?

使用 LocationServices 和 fusedlocation API 另外,您是否为 google 播放服务添加了依赖项?

这是获取当前位置的 tutorial

您还可以找到发送短信的代码here

根据下面的回答

获取位置有两种不同的方式,一种是基于网络,另一种是基于 GPS 服务。当然,基于 GPS 服务的位置总是会更准确。根据文档,使用 GPS 服务接收位置可能需要一些时间。在这种情况下,可以使用最后一个已知位置通过使用 getLastKnownLocation() 方法来获取最近可用的位置。在这里,要获取最后一个可用位置,您应该将 LocationManager.GPS_PROVIDER 更改为 LocationManager.NETWORK_PROVIDER。

如果您希望接收基于 GPS 服务的准确实时位置,我建议您使用 FusedLocationApi。你可以在这里找到它 https://developer.android.com/training/location/receive-location-updates.html