1

我处于必须获取位置的情况,即经度和纬度GSMCellLocation,因此我正在尝试以下方式:

  ...
  TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  location = (GsmCellLocation) tm.getCellLocation();
  cid = location.getCid();
  lac = location.getLac();
  ...

但应用程序崩溃并给了我NPE。我已经通过日志和调试检查了它,它显示getCellLocation返回,null因此我无法获取cidand lac. 来自 Google Docs 的这个方法的签名是这样说明的:

Returns the current location of the device. Return null if current location is not available. 

现在我的问题是,如何在使用此方法之前获取当前位置,使其不为空。GPS因为在我的情况下,我试图将其用作Network获取位置的替代方法。请更新并感谢任何帮助。

4

4 回答 4

2

添加此权限:ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION在您的manifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
于 2013-01-29T07:59:38.143 回答
2

TelephonyManager它可能与 LTE 有关:

如果设备中只有一个无线电并且该无线电具有 LTE 连接,则此方法将返回 null...

于 2015-04-30T21:05:39.210 回答
1

getCellLocation() 返回 null,如果当前位置不可用。请看一下

电话经理

此外,检查手机设置中是否存在基带类型信息。

以下代码对我来说很好用你可以测试一下

      TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
      GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

      int cid = cellLocation.getCid();
      int lac = cellLocation.getLac();
      textGsmCellLocation.setText(cellLocation.toString());
      textCID.setText("gsm cell id: " + String.valueOf(cid));
      textLAC.setText("gsm location area code: " + String.valueOf(lac));
于 2013-01-29T07:51:34.530 回答
0

在清单文件中,我添加了粗略、精细和读取手机状态并在设备上运行的权限。

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

现在添加以下代码:

private void check_ReadPhoneState_Permission() {
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.READ_PHONE_STATE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

            // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.ACCESS_FINE_LOCATION)
                && ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.ACCESS_COARSE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            new AlertDialog.Builder(this)
                    .setTitle(R.string.title_location_permission)
                    .setMessage(R.string.text_location_permission)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //Prompt the user once explanation has been shown
                            ActivityCompat.requestPermissions(PhoneDetailsMain.this,
                                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                                    MY_PERMISSIONS_REQUEST_LOCATION);

                            ActivityCompat.requestPermissions(PhoneDetailsMain.this,
                                    new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                                    MY_PERMISSIONS_REQUEST_COARSE_LOCATION);
                        }
                    })
                    .create()
                    .show();


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_COARSE_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 100: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        }

        // other 'case' lines to check for other
        // permissions this app might request
        case 200: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        android.Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                }

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        }
        case 300: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        android.Manifest.permission.ACCESS_COARSE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                }

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        }
    }
}
于 2017-08-23T11:21:45.810 回答