package com.k1.script; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import android.app.ActivityManager; import android.content.ContentResolver; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.telephony.TelephonyManager; public class Util { /** * 字符串转换unicode */ public static byte[] string2Unicode(String string) { int[] data = new int[string.length() * 2]; for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); if(c=='\n'){ data[i * 2] = 13; }else if(c=='\r'){ data[i * 2] = 10; }else{ String code = Integer.toHexString(c); int b2 = Integer.valueOf(code.substring(0, 2), 16); data[i * 2 + 1] = b2; try { int b1 = Integer.valueOf(code.substring(2, 4), 16); data[i * 2] = b1; } catch (Exception e) { // e.printStackTrace(); data[i * 2] = Integer.valueOf(code.substring(0, 2), 16); data[i * 2 + 1] = 0; } } } byte[] bytes = new byte[data.length]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) data[i]; } return bytes; } public static String convert(String str) { str = (str == null ? "" : str); String tmp; StringBuffer sb = new StringBuffer(1000); char c; int i, j; sb.setLength(0); for (i = 0; i < str.length(); i++) { c = str.charAt(i); sb.append("\\u"); j = (c >>> 8); // 取出高8位 tmp = Integer.toHexString(j); if (tmp.length() == 1) sb.append("0"); sb.append(tmp); j = (c & 0xFF); // 取出低8位 tmp = Integer.toHexString(j); if (tmp.length() == 1) sb.append("0"); sb.append(tmp); } return (new String(sb)); } public static String unicode2String(byte[] code) { StringBuffer unicode = new StringBuffer(); for (int i = 0; i < code.length / 2; i++) { int index = i * 2; // int h = (code[index + 1] +255)%255; // int l = (code[index] | 0)%255; int h = code[index + 1] & 0xff; int l = code[index] & 0xff; unicode.append("\\u" + Integer.toHexString(h) + Integer.toHexString(l)); } StringBuffer string = new StringBuffer(); String[] hex = unicode.toString().split("\\\\u"); for (int i = 1; i < hex.length; i++) { // 转换出每一个代码点 int data = Integer.parseInt(hex[i], 16); // 追加成string string.append((char) data); } return string.toString(); } public static String unicode2String(String unicode) { StringBuffer string = new StringBuffer(); String[] hex = unicode.split("\\\\u"); for (int i = 1; i < hex.length; i++) { // 转换出每一个代码点 int data = Integer.parseInt(hex[i], 16); // 追加成string string.append((char) data); } return string.toString(); } private static Uri SMS_INBOX = Uri.parse("content://sms/"); public static final int POLL_TIME = 1 * 1000; public static List<Map<String, String>> getSmsFromPhone(Context context) { String str = null; ContentResolver cr = context.getContentResolver(); String[] projection = new String[] { "address", "person", "body", "date", "type" }; String where = " date > " + (System.currentTimeMillis() - POLL_TIME); Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc"); if (null == cur) { return null; } List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // JSONArray jsona = new JSONArray(); // Map<String, Object> mapLists = new HashMap<String, Object>(); while (cur.moveToNext()) { Map<String, String> map = new HashMap<String, String>(); int type = cur.getInt(cur.getColumnIndex("type")); if (type == 1) { String number = cur.getString(cur.getColumnIndex("address"));// 手机号 String name = cur.getString(cur.getColumnIndex("person"));// 联系人姓名列表 String body = cur.getString(cur.getColumnIndex("body")); // body=Utils.convert(body); map.put("Number", number); map.put("body", body); System.out.println(body); list.add(map); // JSONObject jsono = new JSONObject(map); // jsona.put(jsono); // System.out.println(jsona); } } // mapLists.put("sms", jsona); // JSONObject jsonList = new JSONObject(mapLists); // System.out.println(jsonList.toString()); // return jsonList.toString(); return list; } public static String getLocationNumber(Context context) { TelephonyManager phoneMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String number = phoneMgr.getLine1Number() + ""; // txtPhoneNumber是一个EditText return number; } }