2016-09-11 4 views
0

私はアンドロイドを初めて使いました。私は電話で観測されるすべてのセル情報を収集するプロジェクトに取り組んでいます。私はTelephonyManager.getAllCellInfo()メソッドを使用しましたが、常にnullを返します。Android 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{ 
        ... 
       } 
     } 
    }); 
} 

私はアンドロイド4.4.2レベル17で作業して17にAPIレベルMin設定と私はGSMネットワークから情報を収集しようとしています。

また、私はAndroidManifest.xmlに次の権限を追加しました:私は私の質問の解決策を持っている

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

可能重複http://stackoverflow.com/questions/16541172/getallcellinfo-returns-null-in-android-4-2-1 – Entea

+0

サムスン、Huawei社などのデバイスの中には、これらを持っていません常にnullを返すAPI –

+0

ドキュメントごと:このメソッドは、getCellLocation()を使用するよりも優先されます。ただし、古いデバイスの場合、getAllCellInfo()はnullを返すことがあります。このような場合は、代わりにgetCellLocation()を呼び出す必要があります。また、情報が利用できない場合、nullが返されます。 –

答えて

0

。それはgetAllCellInfo()機能によって置き換えられました。私は、getAllCellInfo()機能とgetNeighboringCellInfo()機能をサポートしていなければならないアンドロイドレベル17で動作していますが、機能はもうサポートされなくなりました。 とにかく、以下が解決策です。

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"); 
      } 
     }); 
    } 
} 
関連する問題