Android开发中使用广播监控的连接变化
来源:爱站网时间:2021-04-05编辑:网友分享
监控状态在很多网络状态中都会发生变化的,而我们需要实时监控的情况,下面爱站技术频道小编带大家一起来了解Android开发中使用广播监控的连接变化,需要的朋友可以参考下文。
监控状态在很多网络状态中都会发生变化的,而我们需要实时监控的情况,下面爱站技术频道小编带大家一起来了解Android开发中使用广播监控的连接变化,需要的朋友可以参考下文。
废话不多说了,直接给大家贴代码了,具体代码如下所示:
package com.lgs.test.testcode.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
/**
* Created by Administrator on 2017/6/26.
*/
public class UsbConnect {
private final static String ACTION = "android.hardware.usb.action.USB_STATE";
public void start(Context context) {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
context.registerReceiver(usBroadcastReceiver, filter);
}
BroadcastReceiver usBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(context, "aciton =" + action, Toast.LENGTH_SHORT).show();
if (action.equals(ACTION)) {
boolean connected = intent.getExtras().getBoolean("connected");
Toast.makeText(context, "aciton =" + connected, Toast.LENGTH_SHORT).show();
if (connected) {
} else {
}
}
}
};
}
以上就是爱站技术频道小编介绍的Android开发中使用广播监控的连接变化,希望可帮助到对此有需要的你,让你足不出户就能获取到专业的知识。
