问题描述
我想,当漫游发生活化检测。到目前为止,我已经使用了下面这段code,而是因为我一直无法对其进行测试,我不知道这是正确
I'm trying to detect when the roaming activation occurs. So far I've used the following piece of code, but because I haven't been able to test it I am unaware of it's correctness
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);
我已经写了这一点,认为为了让漫游激活第一信号单元必须改变。请让我知道我的演绎是否正确与否,如果不是我怎么能做到这一点。
I've written this , thinking that in order for roaming to activate first the signal cell must change. Please let me know whether my deduction is correct or not, and if not how could I accomplish this.
推荐答案
我想你想的NetworkInfo类使用isRoaming()。但首先你要注册一个改变广播监听器:
I think you want to use isRoaming() in NetworkInfo class. But first you want to register a change broadcast listener:
<receiver android:name="YourListener">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
这给你一个动作名称:ConnectivityManager.CONNECTIVITY_ACTION
This gives you an action name: 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();
}
注:似乎有某些版本,其中getActiveNetworkInfo()返回如果漫游一个空的错误。在这里看到:<一href="http://$c$c.google.com/p/android/issues/detail?id=11866">http://$c$c.google.com/p/android/issues/detail?id=11866
[edit: known issue]NOTE: There seems to be a bug on certain versions, where getActiveNetworkInfo() returns null if roaming. See here: http://code.google.com/p/android/issues/detail?id=11866
我希望这有助于!
灵光
这篇关于漫游检测机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!