安卓getAllCellInfo()返回null(Android getAllCellInfo()

2019-09-28 15:23发布

我是新来的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级别17和设定分钟API等级17,我要尽量收集来自GSM网络的信息。

另外,我增加了以下权限AndroidManifest.xml

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

Answer 1:

我有我的问题的解决方案。 多数民众赞成更换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");
            }
        });
    }
}


文章来源: Android getAllCellInfo() returns null