0

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

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

3 回答 3

2

试试这个 :

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" />
于 2013-01-25T19:11:09.027 回答
1

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

这来自android开发者信息。

你可以在这里阅读更多:http: //developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

那应该有显示 toast 通知所需的一切。

于 2013-01-25T19:09:17.173 回答
0

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

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

那应该工作:)

于 2013-01-25T19:02:24.917 回答