package com.github.phoenix.utils; import android.annotation.SuppressLint; import android.app.Activity; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.location.LocationManager; import android.media.AudioManager; import android.net.Uri; import android.os.BatteryManager; import android.provider.Settings; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * 涉及到如:打开WiFi设置,相册,发送短信,拨打电话等 * * @author Phoenix * @date 2016-10-17 15:22 */ public class SystemUtil { private SystemUtil() {} /** * 打开手机WIFI设置界面 * * @param context */ public static void openWifiSetting(Context context) { Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); context.startActivity(intent); } /** * 启动当前应用设置页面 * * @param activity * @param packageName */ public static void openAppSettings(Activity activity, String packageName) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + packageName)); activity.startActivity(intent); } /** * 携带电话号码跳转到拨号界面 * * @param context * @param phoneNumber */ public static void actionDial(Context context, String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)); context.startActivity(intent); } /** * 直接拨打电话 * * @param context * @param phoneNumber */ public static void actionCall(Context context, String phoneNumber) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); context.startActivity(intent); } /** * 向手机发送短信 要加权限 <uses-permissionandroid:name="android.permission.SEND_SMS"/> * * @param context 上下文 * @param content 发送的内容 * @param phoneNumber 电话号码 */ public static void sendSms(Context context, String content, String phoneNumber) { if (TextUtils.isEmpty(content)) { return; } Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto://" + phoneNumber)); intent.putExtra("sms_body", content); context.startActivity(intent); } /** * 打开软键盘 * * @param mEditText 输入框 * @param mContext 上下文 */ public static void openKeyboard(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 关闭软键盘 * * @param mEditText 输入框 */ public static void closeKeyboard(EditText mEditText) { InputMethodManager imm = (InputMethodManager) mEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } /** * 点击空白处收起软键盘 * * @param activity */ public static void setTouchListener(final Activity activity) { activity.getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (activity.getCurrentFocus() != null) { imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); } else { imm.hideSoftInputFromWindow((activity.findViewById(android.R.id.content)).getWindowToken(), 0); } return false; } }); } /** * 检测GPS是否打开 * * @param context * @return */ public static boolean isGpsEnabled(Context context) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); List<String> accessibleProviders = lm.getProviders(true); for (String name : accessibleProviders) { if ("gps".equals(name)) { return true; } } return false; } /** * GPS开关 当前若关则打开 当前若开则关闭 * * @param context */ public static void toggleGPS(Context context) { Intent gpsIntent = new Intent(); gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send(); } catch (PendingIntent.CanceledException e) { e.printStackTrace(); } } /** * 调节系统音量 * * @param context */ public static void holdSystemAudio(Context context) { AudioManager audiomanage = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // 获取系统最大音量 // int maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取当前音量 // int currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_RING); // 设置音量 // audiomanage.setStreamVolume(AudioManager.STREAM_SYSTEM, currentVolume, AudioManager.FLAG_PLAY_SOUND); // 调节音量 // ADJUST_RAISE 增大音量,与音量键功能相同 // ADJUST_LOWER 降低音量 audiomanage.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); } /** * 设置亮度(每30递增) * * @param activity */ public static void setBrightness(Activity activity) { ContentResolver resolver = activity.getContentResolver(); Uri uri = Settings.System.getUriFor("screen_brightness"); int nowScreenBri = getScreenBrightness(activity); nowScreenBri = nowScreenBri <= 225 ? nowScreenBri + 30 : 30; System.out.println("nowScreenBri==" + nowScreenBri); Settings.System.putInt(resolver, "screen_brightness", nowScreenBri); resolver.notifyChange(uri, null); } /** * 获取屏幕的亮度 * * @param activity * @return */ public static int getScreenBrightness(Activity activity) { int nowBrightnessValue = 0; ContentResolver resolver = activity.getContentResolver(); try { nowBrightnessValue = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 判断手机当前是否在充电状态 * * @param context * @return */ public static boolean isBatteryCharging(Context context) { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, filter); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); if ((status == BatteryManager.BATTERY_STATUS_CHARGING) || (status == BatteryManager.BATTERY_STATUS_FULL)) { return true; } return false; } /** * 判断设备是不是手机 * * @param context * @return */ public static boolean isMobile(Context context) { TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return telephony.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE; } /** * 获取当前设备的IMIE(设备ID):<uses-permission android:name="android.permission.READ_PHONE_STATE"/> */ public static String getDeviceId(Context context) { if (!isMobile(context)) { return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); } TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return telephony.getDeviceId(); } /** * 获取IMSI码 * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p> * * @param context 上下文 * @return IMIE码 */ @SuppressLint("HardwareIds") public static String getIMSI(Context context) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return tm != null ? tm.getSubscriberId() : null; } /** * 获取Sim卡运营商名称 * <p>中国移动、如中国联通、中国电信</p> * * @param context 上下文 * @return sim卡运营商名称 */ public static String getSimOperatorName(Context context) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); return tm != null ? tm.getSimOperatorName() : null; } /** * 获取Sim卡运营商名称 * <p>中国移动、如中国联通、中国电信</p> * * @param context 上下文 * @return 移动网络运营商名称 */ public static String getSimOperatorByMnc(Context context) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String operator = tm != null ? tm.getSimOperator() : null; if (operator == null) return null; switch (operator) { case "46000": case "46002": case "46007": return "中国移动"; case "46001": return "中国联通"; case "46003": return "中国电信"; default: return operator; } } /** * 获取手机联系人 * * @param context * @return */ public static List<HashMap<String, String>> getAllContactInfo(Context context) { ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); // 1.获取内容解析者 ContentResolver resolver = context.getContentResolver(); // 2.获取内容提供者的地址:com.android.contacts // raw_contacts表的地址 :raw_contacts // view_data表的地址 : data // 3.生成查询地址 Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri date_uri = Uri.parse("content://com.android.contacts/data"); // 4.查询操作,先查询raw_contacts,查询contact_id // projection : 查询的字段 Cursor cursor = resolver.query(raw_uri, new String[]{"contact_id"}, null, null, null); // 5.解析cursor while (cursor.moveToNext()) { // 6.获取查询的数据 String contact_id = cursor.getString(0); // cursor.getString(cursor.getColumnIndex("contact_id"));//getColumnIndex // : 查询字段在cursor中索引值,一般都是用在查询字段比较多的时候 // 判断contact_id是否为空 if (!TextUtils.isEmpty(contact_id)) {//null "" // 7.根据contact_id查询view_data表中的数据 // selection : 查询条件 // selectionArgs :查询条件的参数 // sortOrder : 排序 // 空指针: 1.null.方法 2.参数为null Cursor c = resolver.query(date_uri, new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{contact_id}, null); HashMap<String, String> map = new HashMap<String, String>(); // 8.解析c while (c.moveToNext()) { // 9.获取数据 String data1 = c.getString(0); String mimetype = c.getString(1); // 10.根据类型去判断获取的data1数据并保存 if (mimetype.equals("vnd.android.cursor.item/phone_v2")) { // 电话 map.put("phone", data1); } else if (mimetype.equals("vnd.android.cursor.item/name")) { // 姓名 map.put("name", data1); } } // 11.添加到集合中数据 list.add(map); // 12.关闭cursor c.close(); } } // 12.关闭cursor cursor.close(); return list; } }