简体   繁体   English

在连接更改时显示敬酒

[英]show toast when connectivity changes

I built an app that streams audio using a webview. 我构建了一个使用Webview流音频的应用程序。 Now I added a check if there's connectivity (or not). 现在,我添加了一个检查(如果没有)。 Using toast I show a message with "true" or "false", but I'd like show a toast only when the connectivity changes. 使用Toast,我会显示一条带有“ true”或“ false”的消息,但是我只想在连接更改时显示Toast。 What should I do? 我该怎么办?

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;

Try this : 尝试这个 :

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

Add this permission in your Manifest : 在您的清单中添加此权限:

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

You should register for android.net.conn.CONNECTIVITY_CHANGE. 您应该注册android.net.conn.CONNECTIVITY_CHANGE。 And show a dialog only on receiving the intent. 并且仅在接收到意图时显示对话框。

You can create an inner class that extends a BroadcastReceiver and shows a toast in it's onReceive method. 您可以创建一个扩展BroadcastReceiver的内部类,并在其onReceive方法中显示吐司。

That should work :) 那应该工作:)

The ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed. 每当连接详细信息已更改时,ConnectivityManager都会广播CONNECTIVITY_ACTION(“ android.net.conn.CONNECTIVITY_CHANGE”)操作。 You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly. 您可以在清单中注册一个广播接收器,以侦听这些更改并相应地恢复(或暂停)您的后台更新。

That comes from the android developer information. 这来自android开发人员信息。

You can read more here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html 您可以在此处了解更多信息: http : //developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

That should have everything you need to show the toast notifications. 那应该具有显示吐司通知所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM