TelephonyManager.getCellLocation() 返回 null?
Posted
技术标签:
【中文标题】TelephonyManager.getCellLocation() 返回 null?【英文标题】:TelephonyManager.getCellLocation() returns null? 【发布时间】:2013-01-29 07:44:38 【问题描述】:我处于必须通过GSMCellLocation
获取位置(即经度和纬度)的情况,因此我正在尝试以下方式:
...
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation) tm.getCellLocation();
cid = location.getCid();
lac = location.getLac();
...
但应用程序崩溃并给了我NPE
。我通过日志和调试检查了它,它显示getCellLocation
返回null
,因此我无法获得cid
和lac
。来自 Google Docs 的这个方法的签名是这样说明的:
Returns the current location of the device. Return null if current location is not available.
现在我的问题是,如何在使用此方法之前获取当前位置,使其不为空。因为在我的情况下,我试图使用它作为GPS
和Network
的替代来获取位置。请更新并感谢任何帮助。
【问题讨论】:
你是在模拟器还是设备上运行它? 是否可以打印日志 【参考方案1】:在您的manifest.xml
中添加此权限:ACCESS_COARSE_LOCATION
或 ACCESS_FINE_LOCATION
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
【讨论】:
我检查了,所有权限都添加了。 你好,我来晚了,但你能解释一下那个输出的意思吗,就像我得到的 [-1,-1,0] 是什么 @Bhanu Sharma 你有没有找到任何解决你问题的方法,如果你找到了让我告诉老兄【参考方案2】:来自TelephonyManager 可能与LTE有关:
如果设备中只有一个无线电并且该无线电具有 LTE 连接,则此方法将返回 null...
【讨论】:
【参考方案3】:getCellLocation() 返回 null,如果当前位置不可用,请查看
Telephonymanager
另外检查手机设置中是否存在基带类型信息。
以下代码对我来说工作正常你可以测试一下
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));
【讨论】:
【参考方案4】:在清单文件中,我添加了粗略、精细和读取手机状态并在设备上运行的权限。
<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.
【讨论】:
以上是关于TelephonyManager.getCellLocation() 返回 null?的主要内容,如果未能解决你的问题,请参考以下文章