Android getAllCellInfo () returns null

I am new to android and I am working on a project that collects all cell information that is observed over the phone. I used method TelephonyManager.getAllCellInfo()

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 am working on android 4.4.2 level 17 and setting the minimum API level to 17. And I am trying to gather information from the GSM network.

Also, I added the following permission AndroidManifest.xml

:

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

      

+1


source to share


1 answer


I have a solution to my question. which replaced function getAllCellInfo()

with function getNeighboringCellInfo()

, although I am working with android level 17 which should support the function getAllCellInfo()

and the function is getNeighboringCellInfo()

no longer supported. Either way, this 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");
            }
        });
    }
}

      

0


source







All Articles