Android getAllCellInfo() returns 空

Android getAllCellInfo() returns null

我是 android 的新手,我从事的项目是收集 phone 观察到的所有细胞信息。我用过TelephonyManager.getAllCellInfo()方法,但总是returnsnull.

我的代码::

public class NetworkCoverageActivity extends AppCompatActivity {
    private String str;
    private TextView TV;
    private Button getCellsInfoBtn;
    private TelephonyManager TM;
    private List<CellInfo> cellInfoList;
    private PhoneStateListener PSL;
    private int event;

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

        TV = (TextView)findViewById(R.id.iv);
        getCellsInfoBtn = (Button)findViewById(R.id.getCellsInfoBtn);
        TM = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        PSL = new PhoneStateListener();
        event = PSL.LISTEN_CELL_INFO | PSL.LISTEN_CELL_LOCATION;

        getCellsInfoBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                TM.listen(PSL, event);
                cellInfoList = TM.getAllCellInfo();

                if(cellInfoList != null)
                    TV.append("cellInfoList = null");
                else{
                    ...
                }
        }
    });
}

我正在 android 4.4.2 级别 17 并将最低 API 级别设置为 17。我尝试从 GSM 网络收集信息。

此外,我向 AndroidManifest.xml 添加了以下权限:

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

我已经找到问题的答案了。 getAllCellInfo() 函数被 getNeighboringCellInfo() 函数取代,虽然我是 运行 android 应该支持 getAllCellInfo() 函数和 getNeighboringCellInfo() 函数的 17 级不再支持。 不管怎样,下面就是解决方法。

package ayad.bslm.com.networkcoverage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class NetworkCoverageActivity extends AppCompatActivity {

    private TextView TV;
    private TelephonyManager TM;
    private List<NeighboringCellInfo> neighboringCellInfoList;

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

        TV = (TextView)findViewById(R.id.iv);
        Button getCellsInfoBtn = (Button)findViewById(R.id.getCellsInfoBtn);
        TM = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        getCellsInfoBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                neighboringCellInfoList = TM.getNeighboringCellInfo();

                if(neighboringCellInfoList == null)
                    TV.setText("neighboringCellInfoList == null\n");
                else
                    TV.setText("There are " + neighboringCellInfoList.size() + " Cells\n");
            }
        });
    }
}