最近项目需要一个一键获取系统状态的功能,这里我写了一个demon简单的介绍一下如何获取系统的电量、手机的网络状况(包括2G、3G、4G)、蓝牙的开关情况等共功能。具体的各个代码的功能我就不详细的讲解ovo。。 因为大部分我都做了注释。好了我就不废话了直接上代码:
首先就是大家关注的实现部分:
public class MainActivity extends Activity implements View.OnClickListener { private WifiManager mWifiManager; private LocationManager mLocationManager; //判断gps的状态 private boolean isGpsOpen; private boolean isBatteryOpen; private boolean isGsmOpen; private Button bt_gps, bt_battery, bt_conn, bt_gsm, bt_bluetooth; private BatteryReceiver batteryReceiver; private IntentFilter intentFilter; private BluetoothAdapter mBluetoothAdapter; private TelephonyManager Tel; private MyPhoneStateListener MyListener; private ConnectionChangeReceiver myReceiver; private boolean isConnect; public static final String SIGNAL_STRENGTH_NONE_OR_UNKNOWN = "无信号"; public static final String SIGNAL_STRENGTH_POOR = "信号差"; public static final String SIGNAL_STRENGTH_MODERATE = "中等的"; public static final String SIGNAL_STRENGTH_GOOD = "信号比较好"; public static final String SIGNAL_STRENGTH_GREAT = "信号非常好"; /** * Network type is unknown */ public static final int NETWORK_TYPE_UNKNOWN = 0; /** * Current network is GPRS */ public static final int NETWORK_TYPE_GPRS = 1; /** * Current network is EDGE */ public static final int NETWORK_TYPE_EDGE = 2; /** * Current network is UMTS */ public static final int NETWORK_TYPE_UMTS = 3; /** * Current network is CDMA: Either IS95A or IS95B */ public static final int NETWORK_TYPE_CDMA = 4; /** * Current network is EVDO revision 0 */ public static final int NETWORK_TYPE_EVDO_0 = 5; /** * Current network is EVDO revision A */ public static final int NETWORK_TYPE_EVDO_A = 6; /** * Current network is 1xRTT */ public static final int NETWORK_TYPE_1xRTT = 7; /** * Current network is HSDPA */ public static final int NETWORK_TYPE_HSDPA = 8; /** * Current network is HSUPA */ public static final int NETWORK_TYPE_HSUPA = 9; /** * Current network is HSPA */ public static final int NETWORK_TYPE_HSPA = 10; /** * Current network is iDen */ public static final int NETWORK_TYPE_IDEN = 11; /** * Current network is EVDO revision B */ public static final int NETWORK_TYPE_EVDO_B = 12; /** * Current network is LTE */ public static final int NETWORK_TYPE_LTE = 13; /** * Current network is eHRPD */ public static final int NETWORK_TYPE_EHRPD = 14; /** * Current network is HSPA+ */ public static final int NETWORK_TYPE_HSPAP = 15; /** * Current network is GSM */ public static final int NETWORK_TYPE_GSM = 16; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); initEvent(); } /** * 初始化对象 */ private void init() { bt_gps = (Button) findViewById(R.id.bt_gps); bt_battery = (Button) findViewById(R.id.bt_battery); bt_cOnn= (Button) findViewById(R.id.bt_conn); bt_gsm = (Button) findViewById(R.id.bt_gsm); bt_bluetooth = (Button) findViewById(R.id.bt_bluetooth); batteryReceiver = new BatteryReceiver(); //注册广播接受者监听系统电池变化 intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); mLocatiOnManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE); /* Update the listener, and start it */ MyListener = new MyPhoneStateListener(); Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); } /** * 初始化事件 */ private void initEvent() { bt_gps.setOnClickListener(this); bt_battery.setOnClickListener(this); bt_conn.setOnClickListener(this); bt_gsm.setOnClickListener(this); bt_bluetooth.setOnClickListener(this); registerReceiver(); } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_battery: batteryState(); break; case R.id.bt_conn: Toast.makeText(this, netWorkState(), Toast.LENGTH_LONG).show(); break; case R.id.bt_gps: Toast.makeText(this, gpsState(), Toast.LENGTH_LONG).show(); break; case R.id.bt_gsm: isGsmOpen = true; Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); break; case R.id.bt_bluetooth: Toast.makeText(this, blueToothState(), Toast.LENGTH_LONG).show(); break; } } /** * 注册广播得到系统电量的值 */ private void batteryState() { isBatteryOpen = true; //注册receiver registerReceiver(batteryReceiver, intentFilter); } /** * 返回gps的状态 */ private String gpsState() { String gpsState = "gps已关闭"; if (mLocationManager != null) { isGpsOpen = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (isGpsOpen) { gpsState = "gps已打开"; } else { gpsState = "gps已关闭"; } } return gpsState; } /** * @return 返回wife连接状态 */ private boolean isWifiOpen() { boolean isWifiOpen = mWifiManager.isWifiEnabled(); return isWifiOpen; } /** * 判断网络状况 * * @return */ private String netWorkState() { String netWorkState = "未知网络"; if (!isConnect) { return netWorkState = "当前无网络"; } else if (isWifiOpen()) { return netWorkState = "无线网络已打开"; } else { switch (Tel.getNetworkType()) { case NETWORK_TYPE_GPRS: case NETWORK_TYPE_GSM: case NETWORK_TYPE_EDGE: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_1xRTT: case NETWORK_TYPE_IDEN: return netWorkState = "当前2g网络"; case NETWORK_TYPE_UMTS: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSUPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_EVDO_B: case NETWORK_TYPE_EHRPD: case NETWORK_TYPE_HSPAP: return netWorkState = "当前3g网络"; case NETWORK_TYPE_LTE: return netWorkState = "当前4g网络"; default: return netWorkState; } } } /** * 判断蓝牙服务 * * @return */ private String blueToothState() { String blueToothState = "蓝牙已关闭"; if (mBluetoothAdapter != null) { boolean isBlueOpen = mBluetoothAdapter.isEnabled(); if (isBlueOpen) { blueToothState = "蓝牙已打开"; } else { blueToothState = "蓝牙已关闭"; } } return blueToothState; } /** * 广播监听电量变化 */ class BatteryReceiver extends BroadcastReceiver { //判断它是否是为电量变化的Broadcast Action @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction()) && isBatteryOpen) { //获取当前电量 int level = intent.getIntExtra("level", 0); //电量的总刻度 int scale = intent.getIntExtra("scale", 100); //把它转成百分比 Toast.makeText(context, "电池电量为" + ((level * 100) / scale) + "%", Toast.LENGTH_LONG).show(); isBatteryOpen = false; } } } /* 开始PhoneState听众 */ private class MyPhoneStateListener extends PhoneStateListener { /* 从得到的信号强度,每个tiome供应商有更新 */ @Override public void onSignalStrengthsChanged(SignalStrength signalStrength) { super.onSignalStrengthsChanged(signalStrength); String level; if (isGsmOpen) { int asu = signalStrength.getGsmSignalStrength(); if (asu <= 2 || asu == 99) level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN; else if (asu >= 12) level = SIGNAL_STRENGTH_GREAT; else if (asu >= 8) level = SIGNAL_STRENGTH_GOOD; else if (asu >= 5) level = SIGNAL_STRENGTH_MODERATE; else level = SIGNAL_STRENGTH_POOR; Toast.makeText(getApplicationContext(), "当前" + level, Toast.LENGTH_SHORT).show(); } isGsmOpen = false; } } ; /** * 广播监听网络变化 */ public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager cOnnectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) { isCOnnect= false; } else { isCOnnect= true; } } } /** * 注册广播 */ private void registerReceiver() { IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); myReceiver = new ConnectionChangeReceiver(); this.registerReceiver(myReceiver, filter); } /** * 注销广播 */ private void unregisterReceiver() { this.unregisterReceiver(myReceiver); } /** * 注销广播 */ @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(); } }接下来我发下权限的部分:
接下来顺便说下我这里有一个bug就是当你是双卡的时候 手机的信号强度是99的也就是说是一个无法识别的状态,也就是说你其实手机两张卡都有信号但是显示的是无信号。懂的也可以教下我如何区分单卡还是双卡一类的知识,google是不提供这方面的接口的。最后发一下效果图:
哈哈~界面有点丑希望大家见谅。