4

デバイスがローミング中かどうかを確認する必要があるアンドロイドでアプリを開発しています。このコードを使用すると、

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秒ごとにトーストを印刷し続けます。セルの場所が通常のネットワークからローミングに変更された場合にのみ、トーストを印刷したいと考えています。

4

1 に答える 1

0

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

このレシーバーをマニフェストで宣言します:

    <receiver android:name="com.yourproject.service.ConnectivityChangedReceiver"
        android:label="NetworkConnection">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter> 
    </receiver> 
于 2012-10-06T10:30:24.710 に答える