连接发生变化时显示吐司

时间:2013-01-25 18:52:42

标签: java android

我构建了一个使用webview流式传输音频的应用。现在我添加了一个检查是否有连接(或不连接)。使用toast我会显示一条带有“true”或“false”的消息,但我想只在连接发生变化时才显示祝酒词。我该怎么办?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

update();

private void update() {
        new Thread() {
            public void run() {
                while (true) {
                    runOnUiThread(new Runnable() {
                         @Override
                         public void run() {
                             isOnline();
                         }
                    });
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } 
            }
        }.start();
        }


    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {

            Toast.makeText(getApplicationContext(), "true",Toast.LENGTH_LONG).show();   
            return true;
        }
        Toast.makeText(getApplicationContext(), "false",Toast.LENGTH_LONG).show();
        return false;

3 个答案:

答案 0 :(得分:3)

试试这个:

public class BroadCastSampleActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            this.registerReceiver(this.mConnReceiver,
                    new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
        private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

                NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

                if(currentNetworkInfo.isConnected()){
                    Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
                }
            }
        };
}

在您的清单中添加此权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 1 :(得分:1)

您应该注册android.net.conn.CONNECTIVITY_CHANGE。并且仅在接收意图时显示对话框。

您可以创建一个扩展BroadcastReceiver的内部类,并在其onReceive方法中显示toast。

这应该有效:)

答案 2 :(得分:1)

  

只要连接详细信息发生更改,ConnectivityManager就会广播CONNECTIVITY_ACTION(“android.net.conn.CONNECTIVITY_CHANGE”)操作。您可以在清单中注册广播接收器以收听这些更改并相应地恢复(或暂停)您的后台更新。

来自Android开发者信息。

您可以在此处阅读更多内容:http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

这应该包含显示祝酒通知所需的一切。