1、获取IP(ipv4)地址
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;
}
2、获取MAC地址
public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
3、获取手机移动运营商
/**
* 需要加入权限<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;
}
4、获取手机接入点名称
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;
}
5、Android获得UA信息
/**
* 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;
}
6、Wifi状态切换
/**
* Wifi状态切换
*/
public void toggleWiFi() {
WifiManager wm = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wm.setWifiEnabled(!wm.isWifiEnabled()); //设置为false时,点击关闭
}
7、通过反射 切换移动数据
/**
* 通过反射 切换移动数据
* 注意相关权限添加
*/
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);
}
8、Android获取屏幕密度
// 获取屏幕密度(方法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.判断手机是否飞行模式
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.设置手机飞行模式
/**
* 设置手机飞行模式
* @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;
}
来源:oschina
链接:https://my.oschina.net/u/131423/blog/203867