在android中漫游检测

kla*_*han 11 android

我正试图检测漫游激活何时发生.到目前为止,我已经使用了以下代码,但由于我无法测试它,我不知道它的正确性

TelephonyManager telephonyManager = TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 

PhoneStateListener cellLocationListener = new PhoneStateListener() {
public void onCellLocationChanged(CellLocation location) {
  if(telephonyManager.isNetworkRoaming()
  {
    Toast.makeText(getApplicationContext(),"in roaming",Toast.LENGTH_LONG).show();
   }
 }
};

telephonyManager.listen(cellLocationListener, PhoneStateListener.LISTEN_CELL_LOCATION);
Run Code Online (Sandbox Code Playgroud)

我写过这个,认为为了首先激活漫游,信号单元必须改变.请告诉我,我的扣除是否正确,如果不是,我怎么能做到这一点.

Emm*_*uel 15

我想你想在NetworkInfo类中使用isRoaming().但首先要注册一个更改广播监听器:

<receiver android:name="YourListener">
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个操作名称:ConnectivityManager.CONNECTIVITY_ACTION

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
//[edit: check for null]
if(ni != null) {
  //...Here check if this is connected and available...
  return ni.isRoaming();
}
Run Code Online (Sandbox Code Playgroud)

[编辑:已知问题]注意:某些版本似乎存在错误,其中getActiveNetworkInfo()在漫游时返回null.请参阅此处:http://code.google.com/p/android/issues/detail?id = 11866

我希望这有帮助!

灵光


小智 14

isNetworkRoaming()如果您想知道是否存在语音漫游,即使接收方被触发,您也可以在TelephonyManager中进行测试android.net.conn.CONNECTIVITY_CHANGE.我认为这也将避免getActiveNetworkInfo()返回null 时的错误.

public class RoamingListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony =
            (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephony.isNetworkRoaming())
            Toast.makeText(context, "Is on TelephonyM Roaming", Toast.LENGTH_LONG).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在语音漫游中是不可能的,但不是在数据漫游中.http://stackoverflow.com/questions/2783120/data-roaming-in-android (4认同)