我在广播接收器的 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();
}
}