0

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

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

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

我想要做的是我想在 wifi 关闭/打开时显示一个 toast 并且单独显示蓝牙

 if WIFI is on  -  "wifi enabled"
 if WIFI is off -   "wifi disabled"
If bluetooth is on - "Bluetooth enabled"
If bluetooth is off - "Bluetooth disabled"

一次只吐司不是全部

如何解决这个问题?

这是我的代码

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

2 回答 2

1

每当 Wifi 状态发生变化时,您的接收器就会被调用。并且您显示了 Wifi 和蓝牙的 Toast。所以它显示了两个 toasts。它不像启用或禁用蓝牙时那样您的 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();
            }  
     }            
}
于 2012-05-31T03:47:20.820 回答
0

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

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();
        }  
    }
}
于 2012-05-31T04:00:23.643 回答