[英]Check whether the device is in roaming or not after 2 seconds in android
I am developing an app in android where i need to check that whether the device is in roaming or not. 我正在开发一个Android应用程序,我需要检查设备是否在漫游。 when i use this code,
当我使用这个代码时,
Handler m = new Handler();
m.postDelayed(new Runnable()
{
public void run()
{
if(telephonyManager.isNetworkRoaming())
{
Toast.makeText(getApplicationContext(), "Network is in Roaming", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Network not in Roaming", Toast.LENGTH_LONG).show();
}
}
}, 2000);
But it keeps on printing the toast after every 2 seconds. 但它每隔2秒就会继续打印吐司。 I want the toast to be printed only when the cell location is changed from normal network to roaming.
我希望仅在将小区位置从普通网络更改为漫游时才打印Toast。
Declare a BroadcastReceiver: 声明BroadcastReceiver:
public class ConnectivityChangedReceiver extends BroadcastReceiver{
@Override
public void onReceive( Context context, Intent intent )
{
//call the method of showing toast on network roaming changed. Make sure that method is in another activity.
}
}
Declare this receiver in manifest : 在清单中声明此接收器:
<receiver android:name="com.yourproject.service.ConnectivityChangedReceiver"
android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.