文档章节

android广播动态监听一般设置的状态

moz1q1
 moz1q1
发布于 2016/07/21 18:26
字数 571
阅读 202
收藏 0
public class SettingReceiver extends BroadcastReceiver {


    public enum NetState {NET_NO, NET_2G, NET_3G, NET_4G, NET_WIFI, NET_UNKNOWN}

    public enum BluetoothState {STATE_OFF, STATE_ON}

    public enum WIFIState {STATE_OFF, STATE_ON}

    public enum GPRSState {STATE_OFF, STATE_ON}

    public enum AirPlaneState {STATE_OFF, STATE_ON}

    public enum RingerState {RINGER_MODE_NORMAL, RINGER_MODE_VIBRATE, RINGER_MODE_SILENT}

    private CallBack mCallBack;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (mCallBack != null) {
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                //蓝牙
                //http://www.cnblogs.com/lesliefang/p/5445684.html
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        mCallBack.getBluetoothState(BluetoothState.STATE_OFF);
                        //case BluetoothAdapter.STATE_TURNING_OFF:
                        break;
                    case BluetoothAdapter.STATE_ON:
                        //case BluetoothAdapter.STATE_TURNING_ON:
                        mCallBack.getBluetoothState(BluetoothState.STATE_ON);
                        break;
                }
            } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
                //网络
                ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                mCallBack.getWIFIState(SystemUtils.isOpenWifi(context) ? WIFIState.STATE_ON : WIFIState.STATE_OFF);
                mCallBack.getGPRSState(SystemUtils.isOpenGPRS(context) ? GPRSState.STATE_ON : GPRSState.STATE_OFF);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
                    switch (networkInfo.getType()) {
                        case ConnectivityManager.TYPE_WIFI:
                            mCallBack.getNetState(NetState.NET_WIFI);
                            break;
                        case ConnectivityManager.TYPE_MOBILE:
                            switch (networkInfo.getSubtype()) {
                                case TelephonyManager.NETWORK_TYPE_GPRS: //联通2g
                                case TelephonyManager.NETWORK_TYPE_CDMA: //电信2g
                                case TelephonyManager.NETWORK_TYPE_EDGE: //移动2g
                                case TelephonyManager.NETWORK_TYPE_1xRTT:
                                case TelephonyManager.NETWORK_TYPE_IDEN:
                                    mCallBack.getNetState(NetState.NET_2G);
                                    break;
                                case TelephonyManager.NETWORK_TYPE_EVDO_A: //电信3g
                                case TelephonyManager.NETWORK_TYPE_UMTS:
                                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                case TelephonyManager.NETWORK_TYPE_HSDPA:
                                case TelephonyManager.NETWORK_TYPE_HSUPA:
                                case TelephonyManager.NETWORK_TYPE_HSPA:
                                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                case TelephonyManager.NETWORK_TYPE_EHRPD:
                                case TelephonyManager.NETWORK_TYPE_HSPAP:
                                    mCallBack.getNetState(NetState.NET_3G);
                                    break;
                                case TelephonyManager.NETWORK_TYPE_LTE:
                                    mCallBack.getNetState(NetState.NET_4G);
                                    break;
                                default:
                                    mCallBack.getNetState(NetState.NET_UNKNOWN);
                            }
                            break;
                        default:
                            mCallBack.getNetState(NetState.NET_UNKNOWN);
                    }
                } else {
                    mCallBack.getNetState(NetState.NET_NO);
                }
            } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                //电量
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);//获得当前电量
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                //获得总电量
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                mCallBack.getCurrentBattery(level);
            } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
                int isAirplaneMode = Settings.System.getInt(context.getContentResolver(), android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
                mCallBack.getAirPlaneState(isAirplaneMode == 0 ? AirPlaneState.STATE_ON : AirPlaneState.STATE_OFF);
            } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
                AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                final int ringerMode = am.getRingerMode();
                switch (ringerMode) {
                    case AudioManager.RINGER_MODE_NORMAL:
                        //normal
                        mCallBack.getRingerState(RingerState.RINGER_MODE_NORMAL);
                        break;
                    case AudioManager.RINGER_MODE_VIBRATE:
                        //vibrate
                        mCallBack.getRingerState(RingerState.RINGER_MODE_VIBRATE);
                        break;
                    case AudioManager.RINGER_MODE_SILENT:
                        mCallBack.getRingerState(RingerState.RINGER_MODE_SILENT);
                        //silent
                        break;
                }
            }
        }
    }

    public void setCallBack(CallBack callBack) {
        this.mCallBack = callBack;
    }

    public interface CallBack {

        public void getBluetoothState(BluetoothState state);

        public void getNetState(NetState state);

        public void getWIFIState(WIFIState state);

        public void getGPRSState(GPRSState state);

        public void getAirPlaneState(AirPlaneState state);

        public void getRingerState(RingerState state);

        public void getCurrentBattery(int level);
    }


    /**
     * 举例
     *
     * @param context
     * @param callBack
     */
    public static void registerReceiver(Context context, CallBack callBack) {
        SettingReceiver settingReceiver = new SettingReceiver();
        IntentFilter bluetoothFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        IntentFilter netFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        IntentFilter airPlaneFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        IntentFilter audioFilter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
        context.registerReceiver(settingReceiver, bluetoothFilter);
        context.registerReceiver(settingReceiver, netFilter);
        context.registerReceiver(settingReceiver, batteryLevelFilter);
        context.registerReceiver(settingReceiver, airPlaneFilter);
        context.registerReceiver(settingReceiver, audioFilter);
        //通过回调获取信息
        settingReceiver.setCallBack(callBack);
    }
}

=======================================================

public class GpsReceiver {

    private LocationManager mLocationManager;

    private Context mContext;

    private final ContentObserver mGpsMonitor = new ContentObserver(null) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            boolean enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (callBack != null) {
                callBack.getGpsState(enabled);
                //return;
            }
        }
    };

    public GpsReceiver(Context context) {
        mContext = context.getApplicationContext();
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    }

//    private volatile static GpsReceiver mGpsReceiver;
//
//    public static GpsReceiver getInstance(Context context) {
//        GpsReceiver temp = mGpsReceiver;
//        if (temp == mGpsReceiver) {
//            synchronized (GpsReceiver.class) {
//                temp = mGpsReceiver;
//                if (temp == null) {
//                    temp = new GpsReceiver(context);
//                    mGpsReceiver = temp;
//                }
//            }
//        }
//        return temp;
//    }

    /**
     * 启动
     */
    public void onStart() {
        mContext.getContentResolver()
                .registerContentObserver(
                        Settings.Secure.getUriFor(Settings.System.LOCATION_PROVIDERS_ALLOWED),
                        false,
                        mGpsMonitor);
    }

    /**
     * 取消
     */
    public void onStop() {
        mContext.getContentResolver().unregisterContentObserver(mGpsMonitor);
    }

    private CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        public void getGpsState(boolean state);
    }

    /**
     * 举例
     *
     * @param context
     * @param callBack
     */
    public static void registerReceiver(Context context, CallBack callBack) {
        GpsReceiver gpsReceiver = new GpsReceiver(context);
        gpsReceiver.onStart();
        gpsReceiver.setCallBack(callBack);
    }
}

 

=====================================================

求解答,目前我不知道怎么获取 同步 和 屏幕旋转 的监听

© 著作权归作者所有

共有 人打赏支持
moz1q1
粉丝 53
博文 349
码字总数 117339
作品 0
东莞
程序员
《Android深入透析》之广播(Broadcast)

概述 在android中,Broadcast作为四大组件之一,被广泛的应用在android程序之间的数据传递。举一个大家都比较熟悉的例子来说明。在车上的时候大家都有收听广播的习惯,广播电台通过发送不同频...

朵朵和糖糖
2014/11/12
0
0
andriod-四大组件之广播Broadcast-短信的收发

我想几乎所有的安卓开发者,第一个接触到的四大组件之一就是activity,而我在之前的文章中也写过, 在activity里面getContext是大佬级别的存在(可以看前几章), 那么四大组件中的广播Broad...

九尾74
05/24
0
0
Android四大基本组件介绍与生命周期

Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器。 一:了解四大基本组件 Activity : 应用程序中,一个Activity通常就是一个单独的...

tianlonn
2014/04/14
0
0
android学习笔记之Intent与BroadcastReceiver(四)

监听安卓本地的Broadcast Intents 比如时区的改变,数据连接状态,短信的接收,来电,底座模式,电池状态等等。 下面的列表介绍的是一些设备状态变化的Actions: 1.ACTIONBOOTCOMPLETED 当你的...

Chanimalx
2013/01/13
0
0
Android:BroadcastReceiver

Broadcast分类 注册方式: 动态广播 在代码中注册receiver 一定要手动在onDestroy()时调用unregisterReveiver()来取消注册 静态广播 在AndroidManifest.xml中注册receiver(主要是设定action)...

大鱼BIG_FISH
2015/10/27
0
0

没有更多内容

加载失败,请刷新页面

加载更多

如何通过 J2Cache 实现分布式 session 存储

做 Java Web 开发的人多数都会需要使用到 session (会话),我们使用 session 来保存一些需要在两个不同的请求之间共享数据。一般 Java 的 Web 容器像 Tomcat、Resin、Jetty 等等,它们会在...

红薯
今天
1
0
C++ std::thread

C++11提供了std::thread类来表示一个多线程对象。 1,首先介绍一下std::this_thread命名空间: (1)std::this_thread::get_id():返回当前线程id (2)std::this_thread::yield():用户接口...

yepanl
今天
2
0
Nignx缓存文件与动态文件自动均衡的配置

下面这段nginx的配置脚本的作用是,自动判断是否存在缓存文件,如果有优先输出缓存文件,不经过php,如果没有,则回到php去处理,同时生成缓存文件。 PHP框架是ThinkPHP,最后一个rewrite有关...

swingcoder
今天
1
0
20180920 usermod命令与用户密码管理

命令 usermod usermod 命令的选项和 useradd 差不多。 一个用户可以属于多个组,但是gid只有一个;除了gid,其他的组(groups)叫做扩展组。 usermod -u 1010 username # 更改用户idusermod ...

野雪球
今天
1
0
Java网络编程基础

1. 简单了解网络通信协议TCP/IP网络模型相关名词 应用层(HTTP,FTP,DNS等) 传输层(TCP,UDP) 网络层(IP,ICMP等) 链路层(驱动程序,接口等) 链路层:用于定义物理传输通道,通常是对...

江左煤郎
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部