1

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

这是一个代码片段:

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";
    }
}

在尝试其他应用程序(如“Network Cell Info lite”)时,这些应用程序会获取所有单元格信息 :(

非常感谢任何帮助。

4

2 回答 2

5

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

如果您想访问getAllCellInfo(),您还需要启用位置服务

于 2018-02-12T11:45:17.103 回答
0
@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 或更高版本。

于 2018-01-04T10:42:29.293 回答