【问题标题】:GPS Location Is Enabled but Cant Find the CoordinatesGPS 定位已启用但无法找到坐标
【发布时间】:2018-03-02 05:29:24
【问题描述】:

我使用下面的代码来获取 Long Lat,它在 GPS 定位打开时工作正常。但问题是。当我关闭 GPS 位置然后再次打开时。它没有向我显示任何日志纬度。

public class GetGPSlocation implements LocationListener {
Context context;
public GetGPSlocation(Context c) {
    context = c;
}
public Location getLocation() {
    if (ActivityCompat.checkSelfPermission(this.context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(context, "Permision Not Granted", Toast.LENGTH_SHORT).show();
        return null;
    }
    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) {

}

我在按钮 CLick Listner 上使用此代码...

     btnSaveAttd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                GetGPSlocation g = new GetGPSlocation(v.getContext());
                Location l = g.getLocation();
                if (l!=null){
                    LAT = l.getLatitude();
                    LON = l.getLongitude();
                }
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                alertDialog.setTitle("Confirmation");
                alertDialog.setMessage("Are you sure you want to Call this doctor?\n\n" +
                                        "DocID: " +DocID.getText().toString()+ "\n"+
                                        "EmpName: " + empName.getText().toString() + "\n"+
                                        "DateTime: " + date +"\n" +
                                        "Time: "+time+"\n"+
                                        "Status : " + spinner.getSelectedItem()+"\n"+
                                        "Location: "+LON +" , " +LAT);
                alertDialog.setIcon(R.drawable.ic_menu_camera);
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                //endregion
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                alertDialog.show();

           }
        });

任何人都可以告诉我,如果 GPS 被禁用,有什么方法可以自动启用 GPS 定位?

【问题讨论】:

    标签: android gps


    【解决方案1】:

    您可以使用广播接收器收听提供商(即 GPS)的变化:

    registerReceiver(mReceiverCallback, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
    
    private BroadcastReceiver mReceiverCallback = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
                // Do whatever you want here
            }
        }
    };
    

    【讨论】:

    • 没有得到你的答案。
    • 有许多提供位置的提供者,例如:GPS、Wifi、Fused... 当用户打开/关闭位置时,意味着他们正在打开/关闭提供者 这个想法是听任何这些提供者的变化。
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多