在安卓模式下 2 秒后检查设备是否处于漫游状态



我正在开发一个安卓应用程序,我需要检查设备是否处于漫游状态。当我使用此代码时,

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);

但它每 2 秒后继续打印吐司。我希望仅在单元格位置从正常网络更改为漫游时才打印 toast。

声明一个广播接收器:

  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. 
  }
  }

在清单中声明此接收器:

    <receiver android:name="com.yourproject.service.ConnectivityChangedReceiver"
        android:label="NetworkConnection">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter> 
    </receiver> 

相关内容

  • 没有找到相关文章

最新更新