android - 将当前位置作为短信发送 Android Studio

标签 android gps location

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

    }
}

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

最佳答案

根据下面的回答 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

关于android - 将当前位置作为短信发送 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167351/

相关文章:

android - 以编程方式启用和禁用自动旋转?

来自使用 javascript 的 Web 浏览器的 Android native 代码

android - .getLastLocation() FusedLocationProviderClient 的位置数据为空

button - 将 JavaFX 按钮定位在特定位置

java - Android - 使用 webView 将 url 从 Activity 1 发送到 Activity 2

安卓工作室 3.5 警告 : The rule `-keep public class * extends androidx.versionedparcelable.VersionedParcelable { <init>(); }` uses extends

android - 当我退出程序时以编程方式关闭 GPS

iphone - 查找最接近我当前 GPS 位置的坐标

iOS 和 RESTful 网络服务加密

location - 还有其他类似 freegeoip.net 的网站吗