安卓获取双IMEI

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 import android.content.Context;
  5 import android.telephony.TelephonyManager;
  6 import android.widget.Toast;
  7 
  8 public class getImei {
  9     Context context = null;
 10 
 11     public getImei(Context _context) {
 12         context = _context;
 13     }
 14 
 15     public String getimei() {
 16 
 17         try {
 18 
 19             TelephonyManager tm = (TelephonyManager) context
 20                     .getSystemService(Context.TELEPHONY_SERVICE);
 21             String imei = tm.getDeviceId();
 22             List<String> IMEIS = new ArrayList<String>();
 23             if (checkimei(imei.trim())) {
 24                 IMEIS.add(imei.trim());
 25             }
 26             
 27             
 28             try{
 29                 TelephonyManager telephonyManager1 = (TelephonyManager)context.getSystemService("phone1");
 30                 String imeiphone1=   telephonyManager1.getDeviceId();
 31                 if (imeiphone1 != null && checkimei(imeiphone1)) { 
 32                     if (!IMEIS.contains(imeiphone1)) {
 33                         IMEIS.add(imeiphone1); 
 34                         }
 35                 }
 36             }  catch (Exception e) {
 37                          
 38             }
 39             try{
 40                 TelephonyManager telephonyManager2 = (TelephonyManager)context.getSystemService("phone2");
 41                 String imeiphone2=   telephonyManager2.getDeviceId();
 42                 if (imeiphone2 != null && checkimei(imeiphone2)) { 
 43                     if (!IMEIS.contains(imeiphone2)) {
 44                         IMEIS.add(imeiphone2); 
 45                     }
 46                 }
 47                 }  catch (Exception e) {
 48                      
 49             }
 50          
 51             List<String> imeis = IMEI_initQualcommDoubleSim();
 52             if (imeis != null && imeis.size() > 0) {
 53                 for (String item : imeis) {
 54                     if (!IMEIS.contains(item)) {
 55                         IMEIS.add(item);
 56                     }
 57                 }
 58             }
 59 
 60             imeis = IMEI_initMtkSecondDoubleSim();
 61             if (imeis != null && imeis.size() > 0) {
 62                 for (String item : imeis) {
 63                     if (!IMEIS.contains(item)) {
 64                         IMEIS.add(item);
 65                     }
 66                 }
 67             }
 68             imeis = IMEI_initMtkDoubleSim();
 69             if (imeis != null && imeis.size() > 0) {
 70                 for (String item : imeis) {
 71                     if (!IMEIS.contains(item)) {
 72                         IMEIS.add(item);
 73                     }
 74                 }
 75             }
 76             imeis = IMEI_initSpreadDoubleSim();
 77             if (imeis != null && imeis.size() > 0) {
 78                 for (String item : imeis) {
 79                     if (!IMEIS.contains(item)) {
 80                         IMEIS.add(item);
 81                     }
 82                 }
 83             }
 84 
 85             StringBuffer IMEI_SB = new StringBuffer();
 86 
 87             Integer TIMES_TEMP = 1;
 88             for (String item : IMEIS) {
 89                 if (TIMES_TEMP > 1) {
 90                     IMEI_SB.append('
');
 91                 }
 92                 IMEI_SB.append(item);
 93                 // params.put("IMEI" + TIMES_TEMP, item);
 94                 TIMES_TEMP++;
 95             }
 96 
 97             String imeis_tmp = IMEI_SB.toString().trim();
 98             
 99             
100             if ("".equals(imeis_tmp)) {
101                 imeis_tmp = "no_imei_1";
102             }
103             return imeis_tmp;
104             //
105             // TextView_imei.setText(IMEI_SB.toString());
106             //
107             // WriteFile("imei", IMEI_SB.toString());
108 
109         } catch (Exception e) {
110             Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
111             return "no_imei_2";
112         }
113         
114     }
115 
116     private Boolean checkimeisame(String imei) {
117         char firstchar = '0';
118         if (imei.length() > 0) {
119             firstchar = imei.charAt(0);
120         }
121         Boolean issame = true;
122         for (int i = 0; i < imei.length(); i++) {
123             char ch = imei.charAt(i);
124             if (firstchar != ch) {
125                 issame = false;
126                 break;
127             }
128         }
129         return issame;
130         // if (issame) {
131         // // 全是相同字符;
132         // } else {
133         // // 包含不同字符
134         // }
135 
136     }
137 
138     private Boolean checkimei(String IMEI) {
139         Integer LEN = IMEI.length();
140         if (LEN > 10 && LEN < 20 && !checkimeisame(IMEI.trim())) {
141             return true;
142         }
143         return false;
144     }
145 
146     private List<String> IMEI_initMtkDoubleSim() {
147         try {
148             TelephonyManager tm = (TelephonyManager) context
149                     .getSystemService(Context.TELEPHONY_SERVICE);
150             Class<?> c = Class.forName("com.android.internal.telephony.Phone");
151             Integer simId_1, simId_2;
152             try {
153                 java.lang.reflect.Field fields1 = c.getField("GEMINI_SIM_1");
154                 fields1.setAccessible(true);
155                 simId_1 = (Integer) fields1.get(null);
156                 java.lang.reflect.Field fields2 = c.getField("GEMINI_SIM_2");
157                 fields2.setAccessible(true);
158                 simId_2 = (Integer) fields2.get(null);
159             } catch (Exception ex) {
160                 simId_1 = 0;
161                 simId_2 = 1;
162             }
163 
164             // java.lang.reflect.Method m = TelephonyManager.class
165             // .getDeclaredMethod("getSubscriberIdGemini", int.class);
166             // String imsi_1 = (String) m.invoke(tm, simId_1);
167             // String imsi_2 = (String) m.invoke(tm, simId_2);
168 
169             java.lang.reflect.Method m1 = TelephonyManager.class
170                     .getDeclaredMethod("getDeviceIdGemini", int.class);
171             String imei_1 = ((String) m1.invoke(tm, simId_1)).trim();
172             String imei_2 = ((String) m1.invoke(tm, simId_2)).trim();
173 
174             // java.lang.reflect.Method mx = TelephonyManager.class
175             // .getDeclaredMethod("getPhoneTypeGemini", int.class);
176             // Integer phoneType_1 = (Integer) mx.invoke(tm, simId_1);
177             // Integer phoneType_2 = (Integer) mx.invoke(tm, simId_2);
178             // String defaultImsi = "";
179             // if (TextUtils.isEmpty(imsi_1) && (!TextUtils.isEmpty(imsi_2))) {
180             // defaultImsi = imsi_2;
181             // }
182             // if (TextUtils.isEmpty(imsi_2) && (!TextUtils.isEmpty(imsi_1))) {
183             // defaultImsi = imsi_1;
184             // }
185 
186             List<String> imeis = new ArrayList<String>();
187             if (checkimei(imei_1)) {
188                 imeis.add(imei_1);
189             }
190             if (checkimei(imei_2)) {
191                 imeis.add(imei_2);
192             }
193             return imeis;
194         } catch (Exception e) {
195             // ��MTK
196             return null;
197         }
198 
199     }
200 
201     private List<String> IMEI_initMtkSecondDoubleSim() {
202         try {
203             TelephonyManager tm = (TelephonyManager) context
204                     .getSystemService(Context.TELEPHONY_SERVICE);
205             Class<?> c = Class.forName("com.android.internal.telephony.Phone");
206 
207             Integer simId_1, simId_2;
208             try {
209                 java.lang.reflect.Field fields1 = c.getField("GEMINI_SIM_1");
210                 fields1.setAccessible(true);
211                 simId_1 = (Integer) fields1.get(null);
212                 java.lang.reflect.Field fields2 = c.getField("GEMINI_SIM_2");
213                 fields2.setAccessible(true);
214                 simId_2 = (Integer) fields2.get(null);
215             } catch (Exception ex) {
216                 simId_1 = 0;
217                 simId_2 = 1;
218             }
219 
220             java.lang.reflect.Method mx = TelephonyManager.class.getMethod(
221                     "getDefault", int.class);
222             TelephonyManager tm1 = (TelephonyManager) mx.invoke(tm, simId_1);
223             TelephonyManager tm2 = (TelephonyManager) mx.invoke(tm, simId_2);
224 
225             // String imsi_1 = tm1.getSubscriberId();
226             // String imsi_2 = tm2.getSubscriberId();
227 
228             String imei_1 = (tm1.getDeviceId()).trim();
229             String imei_2 = (tm2.getDeviceId()).trim();
230             //
231             // Integer phoneType_1 = tm1.getPhoneType();
232             // Integer phoneType_2 = tm2.getPhoneType();
233             // String defaultImsi = "";
234             // if (TextUtils.isEmpty(imsi_1) && (!TextUtils.isEmpty(imsi_2))) {
235             // defaultImsi = imsi_2;
236             // }
237             // if (TextUtils.isEmpty(imsi_2) && (!TextUtils.isEmpty(imsi_1))) {
238             // defaultImsi = imsi_1;
239             // }
240 
241             List<String> imeis = new ArrayList<String>();
242             if (checkimei(imei_1)) {
243                 imeis.add(imei_1);
244             }
245             if (checkimei(imei_2)) {
246                 imeis.add(imei_2);
247             }
248             return imeis;
249 
250         } catch (Exception e) {
251             return null;
252         }
253     }
254 
255     private List<String> IMEI_initSpreadDoubleSim() {
256         try {
257             Class<?> c = Class
258                     .forName("com.android.internal.telephony.PhoneFactory");
259             java.lang.reflect.Method m = c.getMethod("getServiceName",
260                     String.class, int.class);
261             String spreadTmService = (String) m.invoke(c,
262                     Context.TELEPHONY_SERVICE, 1);
263 
264             TelephonyManager tm = (TelephonyManager) context
265                     .getSystemService(Context.TELEPHONY_SERVICE);
266             // String imsi_1 = tm.getSubscriberId();
267             String imei_1 = (tm.getDeviceId()).trim();
268             // Integer phoneType_1 = tm.getPhoneType();
269             TelephonyManager tm1 = (TelephonyManager) context
270                     .getSystemService(spreadTmService);
271             // String imsi_2 = tm1.getSubscriberId();
272             String imei_2 = (tm1.getDeviceId()).trim();
273             // Integer phoneType_2 = tm1.getPhoneType();
274             // String defaultImsi = "";
275             // if (TextUtils.isEmpty(imsi_1) && (!TextUtils.isEmpty(imsi_2))) {
276             // defaultImsi = imsi_2;
277             // }
278             // if (TextUtils.isEmpty(imsi_2) && (!TextUtils.isEmpty(imsi_1))) {
279             // defaultImsi = imsi_1;
280             // }
281 
282             List<String> imeis = new ArrayList<String>();
283             if (checkimei(imei_1)) {
284                 imeis.add(imei_1);
285             }
286             if (checkimei(imei_2)) {
287                 imeis.add(imei_2);
288             }
289             return imeis;
290 
291         } catch (Exception e) {
292             return null;
293         }
294     }
295 
296     public List<String> IMEI_initQualcommDoubleSim() {
297         try {
298             TelephonyManager tm = (TelephonyManager) context
299                     .getSystemService(Context.TELEPHONY_SERVICE);
300             Class<?> cx = Class
301                     .forName("android.telephony.MSimTelephonyManager");
302             Object obj = context.getSystemService("phone_msim");
303             Integer simId_1 = 0;
304             Integer simId_2 = 1;
305             //
306             // java.lang.reflect.Method mx = cx.getMethod("getDataState");
307             // // int stateimei_1 = (Integer) mx.invoke(cx.newInstance());
308             // int stateimei_2 = tm.getDataState();
309             // java.lang.reflect.Method mde = cx.getMethod("getDefault");
310             java.lang.reflect.Method md = cx
311                     .getMethod("getDeviceId", int.class);
312             // java.lang.reflect.Method ms = cx.getMethod("getSubscriberId",
313             // int.class);
314             // java.lang.reflect.Method mp = cx.getMethod("getPhoneType");
315 
316             // Object obj = mde.invoke(cx);
317 
318             String imei_1 = ((String) md.invoke(obj, simId_1)).trim();
319             String imei_2 = ((String) md.invoke(obj, simId_2)).trim();
320 
321             // String imsi_1 = (String) ms.invoke(obj, simId_1);
322             // String imsi_2 = (String) ms.invoke(obj, simId_2);
323 
324             // int statephoneType_1 = tm.getDataState();
325             // int statephoneType_2 = (Integer) mx.invoke(obj);
326 
327             List<String> imeis = new ArrayList<String>();
328             if (checkimei(imei_1)) {
329                 imeis.add(imei_1);
330             }
331             if (checkimei(imei_2)) {
332                 imeis.add(imei_2);
333             }
334             return imeis;
335 
336             // Log.e("tag", statephoneType_1 + "---" + statephoneType_2);
337 
338             // Class<?> msc = Class.forName("android.telephony.MSimSmsManager");
339             // for (Method m : msc.getMethods()) {
340             // if (m.getName().equals("sendTextMessage")) {
341             // m.getParameterTypes();
342             // }
343             // Log.e("tag", m.getName());
344             // }
345 
346         } catch (Exception e) {
347             return null;
348         }
349     }
350 
351 }
原文地址:https://www.cnblogs.com/cejay/p/3566396.html