一、獲取IP(ipv4)地址java
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) { //!inetAddress.isLinkLocalAddress() Android4.0以上排除IPv6地址 return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; }
二、獲取MAC地址android
public String getLocalMacAddress() { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); }
三、獲取手機移動運營商web
/** * 須要加入權限<uses-permission android:name="android.permission.READ_PHONE_STATE"/> */ public String getProvidersName() { String ProvidersName = null; String IMSI = telephonyManager.getSubscriberId(); // IMSI號前面3位460是國家,緊接着後面2位00 02 07是中國移動,01是中國聯通,03是中國電信。 if (IMSI.startsWith("46000") || IMSI.startsWith("46002") || IMSI.startsWith("46007")) { ProvidersName = "中國移動"; } else if (IMSI.startsWith("46001")) { ProvidersName = "中國聯通"; } else if (IMSI.startsWith("46003")) { ProvidersName = "中國電信"; } return ProvidersName; }
四、獲取手機接入點名稱ide
public static String getApnType(Context context) { String apntype = "nomatch"; try { Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); c.moveToFirst(); return c.getString(c.getColumnIndex("apn")); } catch (Exception e) { e.printStackTrace(); } return apntype; }
五、Android得到UA信息oop
/** * Android得到UA信息 * * @param ctx * @return */ public static String getUserAgentString(Context ctx) { WebView webview; webview = new WebView(ctx); webview.layout(0, 0, 0, 0); WebSettings settings = webview.getSettings(); String ua = settings.getUserAgentString(); Log.i("UA", ua); return ua; }
六、Wifi狀態切換this
/** * Wifi狀態切換 */ public void toggleWiFi() { WifiManager wm = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE); wm.setWifiEnabled(!wm.isWifiEnabled()); //設置爲false時,點擊關閉 }
七、經過反射 切換移動數據3d
/** * 經過反射 切換移動數據 * 注意相關權限添加 */ private void switchMobileData() { Object[] arg = null; try { boolean isMobileDataEnable = invokeMethod("getMobileDataEnabled", arg); //獲取移動數據狀態 if (!isMobileDataEnable) { invokeBooleanArgMethod("setMobileDataEnabled", true); //開啓移動數據 } else { invokeBooleanArgMethod("setMobileDataEnabled", false); //關閉移動數據 } } catch (Exception e) { e.printStackTrace(); } } public boolean invokeMethod(String methodName, Object[] arg) throws Exception { ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Class ownerClass = mConnectivityManager.getClass(); Class[] argsClass = null; if (arg != null) { argsClass = new Class[1]; argsClass[0] = arg.getClass(); } Method method = ownerClass.getMethod(methodName, argsClass); Boolean isOpen = (Boolean) method.invoke(mConnectivityManager, arg); return isOpen; } public Object invokeBooleanArgMethod(String methodName, boolean value) throws Exception { ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Class ownerClass = mConnectivityManager.getClass(); Class[] argsClass = new Class[1]; argsClass[0] = boolean.class; Method method = ownerClass.getMethod(methodName, argsClass); return method.invoke(mConnectivityManager, value); }
八、Android獲取屏幕密度code
// 獲取屏幕密度(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕寬(像素,如:480px) int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p) Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); // 獲取屏幕密度(方法2) DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) float xdpi = dm.xdpi; float ydpi = dm.ydpi; Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi); Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI); screenWidth = dm.widthPixels; // 屏幕寬(像素,如:480px) screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px) Log.e(TAG + " DisplayMetrics", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); // 獲取屏幕密度(方法3) dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) xdpi = dm.xdpi; ydpi = dm.ydpi; Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi); Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI); int screenWidthDip = dm.widthPixels; // 屏幕寬(dip,如:320dip) int screenHeightDip = dm.heightPixels; // 屏幕寬(dip,如:533dip) Log.e(TAG + " DisplayMetrics", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip); screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕寬(px,如:480px) screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px) Log.e(TAG + " DisplayMetrics", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
9.判斷手機是否飛行模式ip
public static boolean getAirplaneMode(Context context){ int isAirplaneMode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) ; return (isAirplaneMode == 1)?true:false;
10.設置手機飛行模式webview
/** * 設置手機飛行模式 * @param context * @param enabling true:設置爲飛行模式 false:取消飛行模式 */ public static void setAirplaneModeOn(Context context,boolean enabling) { Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,enabling ? 1 : 0); Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); intent.putExtra("state", enabling); context.sendBroadcast(intent); }
11.亮度控制
//------------省電亮度調節------------------- public static void setBrightness(Activity activity, int brightness) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); // Log.d("lxy", "set lp.screenBrightness == " + lp.screenBrightness); activity.getWindow().setAttributes(lp); //save ContentResolver resolver = activity.getContentResolver(); saveBrightness( resolver, brightness); } public static void saveBrightness(ContentResolver resolver, int brightness) { Uri uri = android.provider.Settings.System.getUriFor("screen_brightness"); android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness); resolver.notifyChange(uri, null); } public static int getScreenBrightness(Activity activity) { int nowBrightnessValue = 0; ContentResolver resolver = activity.getContentResolver(); try{ nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); } catch(Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 中止自動亮度調節 */ public static void stopAutoBrightness(Activity activity) { Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } /** * 開啓亮度自動調節 * * @param activity */ public static void startAutoBrightness(Activity activity) { Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); } /** * 判斷是否開啓了自動亮度調節 */ public static boolean isAutoBrightness(ContentResolver aContentResolver) { boolean automicBrightness = false; try{ automicBrightness = Settings.System.getInt(aContentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch(SettingNotFoundException e) { e.printStackTrace(); } return automicBrightness; }