简体   繁体   English

在android中2秒后检查设备是否处于漫游状态

[英]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.

相关问题 Android 5.0 (L) - 检查数据漫游设置 - Android 5.0 (L) - Check data Roaming Setting 如何检查Android设备是否能够进行触觉反馈? - How to check whether an android device is capable of haptic feedback? 如何检查特定设备是否支持Android中的4G网络? - How to check whether a particular device supports 4G networks in Android? 以编程方式检查我的 android 设备是否已激活工作资料? - Programatically Check whether my android device has activated work profile or not? 如何检查android设备是否可以连接到网络? - How to check whether the android device can connecto to the network or not? 如何以编程方式检查android设备是否支持DLNA - How to check whether an android device supports DLNA or not programmatically Android App 如何检查设备是空闲还是移动? - How to check whether the device is idle or moving for Android App? 如何检查 android 设备是否支持 gps? - How to check whether an android device has gps support or not? 如何检查Android设备是否连接到网络? - How can i check whether an android device is connected to the web? 如何以编程方式检查Android设备是否支持OTG功能 - How to check whether Android device supports OTG features programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM