Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在广播接收器的onreceive()中一次显示一个祝酒词而不是一次显示所有祝酒词_Android_Bluetooth_Broadcastreceiver_Wifi_Toast - Fatal编程技术网

Android 如何在广播接收器的onreceive()中一次显示一个祝酒词而不是一次显示所有祝酒词

Android 如何在广播接收器的onreceive()中一次显示一个祝酒词而不是一次显示所有祝酒词,android,bluetooth,broadcastreceiver,wifi,toast,Android,Bluetooth,Broadcastreceiver,Wifi,Toast,在广播接收器的OnReceive()中限制toast消息时遇到了一个奇怪的问题 我已经编写了在启用/禁用wifi和蓝牙时显示toast的代码 但如果我启用/禁用wifi,蓝牙toast也会出现,反之亦然,即当我启用/禁用蓝牙和wifi时,所有4条toast消息都会逐个出现 我想做的是,当wifi关闭/打开时,我想为蓝牙单独举杯 像 一次只烤一杯,不是全部 如何解决这个问题 这是我的密码 public class MyService extends BroadcastReceiver { pr

在广播接收器的OnReceive()中限制toast消息时遇到了一个奇怪的问题

我已经编写了在启用/禁用wifi和蓝牙时显示toast的代码

但如果我启用/禁用wifi,蓝牙toast也会出现,反之亦然,即当我启用/禁用蓝牙和wifi时,所有4条toast消息都会逐个出现

我想做的是,当wifi关闭/打开时,我想为蓝牙单独举杯

一次只烤一杯,不是全部

如何解决这个问题

这是我的密码

  public class MyService extends BroadcastReceiver {
private WifiManager wifiManager;
@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub

    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    if(wifiManager.isWifiEnabled()){  
        Toast.makeText(context.getApplicationContext(), "WIFI  Enabled", Toast.LENGTH_LONG).show();
    }else {    
        Toast.makeText(context.getApplicationContext(), "WIFI Disabled", Toast.LENGTH_LONG).show();
    }  
    if(adapter.isEnabled()){

        Toast.makeText(context.getApplicationContext(), "Bluetooth Enabled", Toast.LENGTH_LONG).show();

    }else {    
        Toast.makeText(context.getApplicationContext(), " Bluetooth Disabled", Toast.LENGTH_LONG).show();
    }
}

每当Wifi状态发生变化时,就会呼叫您的接收器。您同时显示了Wifi和蓝牙的祝酒词。因此,它会显示两个祝酒词。这与启用或禁用蓝牙不同。您的Wifi没有启用或禁用。Wifi和蓝牙都有启用或禁用状态

为了解决你的问题,你可以像

public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    if(arg1.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))  {  
            wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if(wifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED||wifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLING){  
                  Toast.makeText(context.getApplicationContext(), "WIFI  Enabled", Toast.LENGTH_LONG).show();
            }else {    
                  Toast.makeText(context.getApplicationContext(), "WIFI Disabled", Toast.LENGTH_LONG).show();
            }
    }else {  
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if(adapter.isEnabled()){
                  Toast.makeText(context.getApplicationContext(), "Bluetooth Enabled", Toast.LENGTH_LONG).show();

            }else {    
                  Toast.makeText(context.getApplicationContext(), " Bluetooth Disabled", Toast.LENGTH_LONG).show();
            }  
     }            
}

在代码中有两个不同的if条件,这是显示两个吐司的唯一原因。看看我在这里更新的代码,它会解决你的问题

public class MyService extends BroadcastReceiver 
{
    private WifiManager wifiManager;
    @Override
    public void onReceive(Context context, Intent arg1) 
    {
        // TODO Auto-generated method stub
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

        if(wifiManager.isWifiEnabled() )
        {  
            Toast.makeText(context.getApplicationContext(), "WIFI  Enabled", Toast.LENGTH_LONG).show();
        }
        else if (!wifiManager.isWifiEnabled() ) 
        {    
            Toast.makeText(context.getApplicationContext(), "WIFI Disabled", Toast.LENGTH_LONG).show();
        }  
        else if(adapter.isEnabled() )
        {
            Toast.makeText(context.getApplicationContext(), "Bluetooth Enabled", Toast.LENGTH_LONG).show();
        }
        else if  (!adapter.isEnabled() )
        {    
            Toast.makeText(context.getApplicationContext(), " Bluetooth Disabled", Toast.LENGTH_LONG).show();
        }  
    }
}

谢谢你的回复,但是我应该在哪里更改代码呢?由于我是android新手,我不知道该怎么办。谢谢Rasel,但当我禁用wifi时,它会显示“wifi已禁用”,当我启用wifi时,它会显示“wifi已禁用”,“wifi已启用”它显示两个祝酒词,在蓝牙的情况下显示相同的祝酒词also@Rag这是因为有时需要启用和禁用wifi。如果启用或禁用wifi,则可以显示wifi已启用。否则,您希望一次显示一个烤面包片,对吗?是的,一次显示一个烤面包片好的,了解您的要求@Raghav,请尝试我的答案,您将获得准确的结果无论蓝牙如何,Wi-Fi始终处于启用或禁用状态。因此,他将永远不会获得蓝牙状态的祝酒词。他必须使用意向操作“接收器被触发的动作”进行测试。谢谢,但当我禁用wifi时,其显示为“wifi禁用”,当我启用wifi时,其显示为“wifi禁用”,“wifi启用”它同时显示祝酒词,在蓝牙的情况下也显示相同的祝酒词。我试了两个答案,但结果是一样的
public class MyService extends BroadcastReceiver 
{
    private WifiManager wifiManager;
    @Override
    public void onReceive(Context context, Intent arg1) 
    {
        // TODO Auto-generated method stub
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

        if(wifiManager.isWifiEnabled() )
        {  
            Toast.makeText(context.getApplicationContext(), "WIFI  Enabled", Toast.LENGTH_LONG).show();
        }
        else if (!wifiManager.isWifiEnabled() ) 
        {    
            Toast.makeText(context.getApplicationContext(), "WIFI Disabled", Toast.LENGTH_LONG).show();
        }  
        else if(adapter.isEnabled() )
        {
            Toast.makeText(context.getApplicationContext(), "Bluetooth Enabled", Toast.LENGTH_LONG).show();
        }
        else if  (!adapter.isEnabled() )
        {    
            Toast.makeText(context.getApplicationContext(), " Bluetooth Disabled", Toast.LENGTH_LONG).show();
        }  
    }
}