【问题标题】:Sending current location as SMS Android Studio将当前位置作为 SMS Android Studio 发送
【发布时间】:2017-09-12 03:17:13
【问题描述】:

我是 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 类:

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) {

    }
}

我已在清单中授予权限:精细位置、粗略位置以及接收和发送短信。我做错了什么?

【问题讨论】:

  • 你有位置吗? (在通过短信发送之前)

标签: android gps location


【解决方案1】:

根据以下答案 https://stackoverflow.com/a/43199211/6835152

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

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

【讨论】:

  • 在您的代码中,您没有收到最后一个已知位置
  • 所以我应该将 LocationManager.GPS_PROVIDER 更改为 LocationManager.NETWORK_PROVIDER,这样我就会收到最后一个已知位置?
  • 是的,你会收到位置信息,但我更喜欢使用融合位置 API,因为你必须添加谷歌播放服务,所以它也很容易集成。
【解决方案2】:

使用 LocationServices 和 fusedlocation API 另外,您是否添加了对 google play 服务的依赖?

这是一个tutorial,用于获取当前位置。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多