Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android getAllCellInfo()返回Nexus 5x上的空列表_Android_Telephonymanager_Cellinfo - Fatal编程技术网

Android getAllCellInfo()返回Nexus 5x上的空列表

Android getAllCellInfo()返回Nexus 5x上的空列表,android,telephonymanager,cellinfo,Android,Telephonymanager,Cellinfo,我读了所有关于getAllCellInfo()的帖子,也有类似的情况。 下面发布的代码在Android 6(OnePlus 1)上运行良好,但在Google Nexus 5x Android 8.1 API 27上返回一个空的Cellinfo列表。 我已经在清单中设置了权限访问CORSE位置,并在第一次运行应用程序时请求权限 以下是一段代码片段: try { TelephonyManager tm = (TelephonyManager) mContext.getSystemS

我读了所有关于getAllCellInfo()的帖子,也有类似的情况。 下面发布的代码在Android 6(OnePlus 1)上运行良好,但在Google Nexus 5x Android 8.1 API 27上返回一个空的Cellinfo列表。 我已经在清单中设置了权限访问CORSE位置,并在第一次运行应用程序时请求权限

以下是一段代码片段:

try {

        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String networkOperator = "";
        if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { 
            networkOperator = tm.getNetworkOperator(); 
        }

        if (!TextUtils.isEmpty(networkOperator)) { //is not empty with Nexus 5x
            mcc = networkOperator.substring(0, 3); 
            mnc = networkOperator.substring(3);
        } else {
            mcc = "Unbekannt";
            mnc = "Unbekannt";
        }

        List<CellInfo> cell = tm.getAllCellInfo();
        System.out.println("List: "+cell);
        if (cell != null) { //Always null with Nexus 5x
          //  TODO
        } else {
            cellID = "ERROR";
            lac = "ERROR";
        }
    }
    catch(Exception ex) {
        mcc = "No Permission";
        mnc = "No Permission";
        cellID = "ERROR";
        lac = "ERROR";
    }
}
试试看{
TelephonyManager tm=(TelephonyManager)mContext.getSystemService(Context.TELEPHONY_服务);
字符串networkOperator=“”;
如果(tm.getPhoneType()==TelephonyManager.PHONE_TYPE_GSM){
networkOperator=tm.getNetworkOperator();
}
如果(!TextUtils.isEmpty(networkOperator)){//对于Nexus 5x不为空
mcc=网络操作员。子字符串(0,3);
mnc=网络运营商。子字符串(3);
}否则{
mcc=“Unbekannt”;
mnc=“Unbekannt”;
}
List cell=tm.getAllCellInfo();
System.out.println(“列表:+单元格);
如果(cell!=null){//Nexus 5x始终为null
//待办事项
}否则{
cellID=“错误”;
lac=“错误”;
}
}
捕获(例外情况除外){
mcc=“无许可”;
mnc=“无许可”;
cellID=“错误”;
lac=“错误”;
}
}
尝试其他应用程序(如“Network Cell Info lite”)时,这些应用程序会获取所有手机信息:(

非常感谢您的帮助

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //.........

    checkLocationPermission();
}



 public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

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

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_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(MainActivity.this,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION);
                            }
                        })
                        .create()
                        .show();


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

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // 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,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) {

                        //Request location updates:
                        locationManager.requestLocationUpdates(provider, 400, 1, this);
                    }

                } else {

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

                }
                return;
            }

        }
    }
尝试此操作。可能是因为操作系统版本为23或更高,所以您有权限问题


尝试此操作。可能是因为操作系统版本为23或更高,您有权限问题。

在Android 8.0及更高版本中,要访问
getAllCellInfo()
您的应用程序应该位于前台


如果你想访问
getAllCellInfo()
你也需要启用。

在Android 8.0及以上版本中,要访问
getAllCellInfo()
你的应用程序应该在前台



如果要访问
getAllCellInfo()
您也需要启用。

将targetsdk版本设置为21,并在清单中授予访问位置的权限。您的问题将得到解决。你好,Prateek,谢谢。这很有效。您能解释一下降级targetsdk版本的作用吗?在build.gradle中设置target sdk版本21,而不是在min sdk版本中。如果将target sdk设置为21,则不需要o使用运行时权限的概念。默认情况下,它为SDK 23提供的权限与下面的设备相同。嘿,我们仍在努力使其正常工作。我们将目标SDK设置为21,并且我们很少收到getallcellinfo()返回的单元列表。但在95%的情况下,不会返回cellInfos列表。但是可以进行电话呼叫。因此必须有手机连接。将targetsdk版本设置为21,并在清单中授予访问位置的权限。您的问题将得到解决。嗨,Prateek,谢谢。这很有效。您能解释一下targetsdk版本降级的原因吗?在内部版本中设置targetsdk版本21.gradle不是min sdk版本。如果将目标sdk设置为21,则不需要使用运行时权限的概念。默认情况下,它为sdk 23提供的权限与下面的设备相同。嘿,我们仍在努力实现这一点。我们将目标sdk设置为21,并且我们很少收到getallcellinfo()返回的单元列表.但在95%的情况下,不会返回手机信息列表。但是电话呼叫是可以进行的。因此必须有手机连接。在前台运行应用程序是很棘手的,因为它假设在后台作为服务运行,并在进行呼叫时检查当前的手机发射塔。对于Android 7.1及更低版本的测试设备,位置服务被禁用,并且可以正常工作。这是否也仅适用于8.0及以上版本?是的。定位服务应仅适用于android 8.0及以上版本。在android 8.0以下版本,它将在没有定位服务的情况下工作。@Anand我确实面临类似的问题。我看到谷歌在这里提到了android P的相同问题-但是,我没有找到任何android O的文档。您看到任何文档吗?请提供帮助我有链接吗?@Swati,我没有任何关于它的链接。但我是在Android 8.0的各种设备上开发和测试时了解到这一点的。这是解决我问题的正确方法。谢谢!在前台运行应用程序是很棘手的,因为它应该在后台作为服务运行,并在通话时检查curretn cell towersced。对于Android 7.1及更低版本的测试设备,位置服务已被禁用,并且可以正常工作。这是否也仅适用于8.0及以上版本?是的。位置服务应仅适用于Android 8.0及以上版本。在Android 8.0以下版本,位置服务将在没有位置服务的情况下正常工作。@Anand我确实面临类似问题。我看到谷歌在这里提到了Android P的相同功能-但是,我没有找到Android O的任何文档。你看到任何文档了吗?你能帮我链接吗?@Swati,我没有关于它的任何链接。但我是在Android 8.0的各种设备上开发和测试时了解到这一点的。这是解决我问题的正确方法。谢谢!