我是 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 并将最低 API 级别设置为 17。并且我尝试从 GSM 网络收集信息。
此外,我还在 AndroidManifest.xml
中添加了以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
最佳答案
我的问题已经得到解答了。已替换 getAllCellInfo()
功能由 getNeighboringCellInfo()
功能,虽然我运行的 android level 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");
}
});
}
}
关于java - Android getAllCellInfo() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436761/