Android getAllCellInfo()返回null

时间:2016-09-11 13:49:15

标签: java android

我是android的新手,我正在开发一个项目来收集通过电话观察到的所有细胞信息。我使用了TelephonyManager.getAllCellInfo()方法,但它始终返回null

我的代码::

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 level 17并将min API级别设置为17.我尝试从GSM网络收集信息。

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

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

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。这被getAllCellInfo()函数替换为getNeighboringCellInfo()函数,虽然我运行的是android级别17,应该支持getAllCellInfo()函数,并且不再支持getNeighboringCellInfo()函数。 无论如何,以下是解决方案。

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