Android getAllCellInfo() returns null

2019-07-30 00:08发布

I'm new to android, and I work on a project that goes to collect all cells information that observed by phone. I have used TelephonyManager.getAllCellInfo() method, but it always returns null.

My Code ::

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{
                    ...
                }
        }
    });
}

I'm working on android 4.4.2 level 17 and set min API level to 17. and I try to collect information from GSM network.

Also, I added the following permission to AndroidManifest.xml:

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

1条回答
一纸荒年 Trace。
2楼-- · 2019-07-30 01:04

I have got the solution of my question. that's replaced getAllCellInfo() function by getNeighboringCellInfo() function, although I am running with android level 17 that should be support getAllCellInfo() function and getNeighboringCellInfo() function should be no longer supported. Anyway, the following is the solution.

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");
            }
        });
    }
}
查看更多
登录 后发表回答