Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1570934
  • 博文数量: 113
  • 博客积分: 3526
  • 博客等级: 中校
  • 技术积分: 1815
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-08 09:46
个人简介

记录总结自己的工作

文章分类

全部博文(113)

文章存档

2015年(19)

2014年(10)

2013年(6)

2012年(16)

2011年(24)

2010年(21)

2009年(17)

分类: 嵌入式

2011-06-21 15:17:43

   现在很多人打电话都会使用一个套餐,比较划算。通常一个月有多少分钟免费呼叫,几十到几百条的免费短信。我自己写了一个小软件,可以统计一个月打出的电话总时间,可以帮助使用套餐的同学们把握好免费呼叫的时间。
   一般来讲,android会将通话记录都保存在一个特定的contentprovider中,用手机查看的通话记录也是从里面查询出来的数据。操作这个contentprovider的类就是CallLog类,它有一个内部类Calls,在Calls中对uri等成员都已经做好了定义,可以使用Calls的成员直接进行通话记录的查询。当然,对通话记录的插入也是通过这个类来实现的。
   另外值得一提的是,如果在手机的通话记录中将所有的通话记录都删除了话,是查不出任何数据的。
   程序的界面我采用了AlertDialog的形式,本来实现的功能和要显示的内容就很简单,使用Activity的话感觉有点大。整个程序只有一个类TelTime.java,其实说起来很简单,就是读取出所有的通话记录,然后一条一条地按时间进行分类求和而已。
    1. package org.test;

    2. import android.app.Activity;
    3. import android.app.AlertDialog;
    4. import android.content.ContentResolver;
    5. import android.content.DialogInterface;
    6. import android.database.Cursor;
    7. import android.os.Bundle;
    8. import android.provider.CallLog;
    9. import android.util.Log;
    10. import android.view.View;
    11. import android.widget.TextView;

    12. import java.text.SimpleDateFormat;
    13. import java.util.Date;
    14. import java.util.HashMap;
    15. import java.util.Map;
    16. import java.util.Set;

    17. public class TelTime extends Activity implements DialogInterface.OnCancelListener {
    18.     private TextView outView;

    19.     private TextView inView;

    20.     private TextView out;

    21.     private TextView in;

    22.     View view;

    23.     int type;

    24.     long callTime;

    25.     long totalOutTime = 0;

    26.     int totalOutCall = 0;

    27.     long totalInTime = 0;

    28.     int totalInCall = 0;

    29.     long singleTime = 0;

    30.     AlertDialog dialog;

    31.     Date date;

    32.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

    33.     String nowTime;

    34.     String tagTime = "";

    35.     String time;

    36.     String name;

    37.     Map<String, MonthTime> monthMap = new HashMap<String, MonthTime>();

    38.     Map<String, Integer> mInMap = new HashMap<String, Integer>();

    39.     Map<String, Integer> mOutMap = new HashMap<String, Integer>();

    40.     MonthTime currentMonth;

    41.     public void onCreate(Bundle savedInstanceState) {
    42.         super.onCreate(savedInstanceState);
    43.         // requestWindowFeature(Window.FEATURE_NO_TITLE);
    44.         nowTime = format.format((new Date()));
    45.         ContentResolver cr = getContentResolver();
    46.         final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[] {
    47.                 CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.DURATION,
    48.                 CallLog.Calls.CACHED_NAME
    49.         }, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
    50.         for (int i = 0; i < cursor.getCount(); i++) {
    51.             cursor.moveToPosition(i);
    52.             type = cursor.getInt(0);
    53.             callTime = cursor.getLong(2);
    54.             name = cursor.getString(3);
    55.             date = new Date(Long.parseLong(cursor.getString(1)));
    56.             time = format.format(date);
    57.             if (!time.equals(tagTime)) {
    58.                 if (tagTime.equals("")) {

    59.                 } else {
    60.                     currentMonth.mostInPeople = getMost(mInMap);
    61.                     currentMonth.mostInTimes = mInMap.get(currentMonth.mostInPeople);
    62.                     currentMonth.mostOutPeople = getMost(mOutMap);
    63.                     currentMonth.mostOutTimes = mOutMap.get(currentMonth.mostOutPeople);
    64.                     monthMap.put(tagTime, currentMonth);
    65.                     mInMap.clear();
    66.                     mOutMap.clear();
    67.                 }
    68.                 currentMonth = new MonthTime();
    69.                 tagTime = time;
    70.                 // if (monthMap.containsKey(time)) {
    71.                 // currentMonth = monthMap.get(time);
    72.                 // monthMap.remove(time);
    73.                 // } else {
    74.                 // currentMonth = new MonthTime();
    75.                 // }
    76.             }
    77.             if (callTime % 60 != 0) {
    78.                 singleTime = callTime / 60 + 1;
    79.             } else {
    80.                 singleTime = callTime / 60;
    81.             }
    82.             if (type == 1) {
    83.                 currentMonth.totalInCall++;
    84.                 currentMonth.totalInTime = currentMonth.totalInTime + singleTime;
    85.                 if (mInMap.containsKey(name)) {
    86.                     Integer time = mInMap.get(name);
    87.                     mInMap.remove(name);
    88.                     mInMap.put(name, time + 1);
    89.                 } else {
    90.                     if (name != null) {
    91.                         mInMap.put(name, 1);
    92.                     }
    93.                 }
    94.             }
    95.             if (type == 2) {
    96.                 currentMonth.totalOutCall++;
    97.                 currentMonth.totalOutTime = currentMonth.totalOutTime + singleTime;
    98.                 if (mOutMap.containsKey(name)) {
    99.                     Integer time = mOutMap.get(name);
    100.                     mOutMap.remove(name);
    101.                     mOutMap.put(name, time + 1);
    102.                 } else {
    103.                     if (name != null) {
    104.                         mOutMap.put(name, 1);
    105.                     }
    106.                 }
    107. // Log.d("test", "name:" + name);
    108. // Log.d("test", "time :" + tagTime);
    109.             }
    110.             if (type == 3) {
    111.                 if (mInMap.containsKey(name)) {
    112.                     Integer time = mInMap.get(name);
    113.                     mInMap.remove(name);
    114.                     mInMap.put(name, time + 1);
    115.                 } else {
    116.                     if (name != null) {
    117.                         mInMap.put(name, 1);
    118.                     }
    119.                 }
    120.             }


    121.         }

    122.         showDialog(nowTime);
    123.     }

    124.     public void onStart() {
    125.         super.onStart();

    126.     }

    127.     private void showDialog(final String time) {
    128.         view = getLayoutInflater().inflate(R.layout.main, null);

    129.         outView = (TextView)view.findViewById(R.id.outtime);
    130.         inView = (TextView)view.findViewById(R.id.intime);
    131.         out = (TextView)view.findViewById(R.id.out);
    132.         in = (TextView)view.findViewById(R.id.in);
    133.         if (monthMap.containsKey(time)) {
    134.             inView.setText("本月共接听电话:" + monthMap.get(time).totalInCall + "次\n总接听时间:"
    135.                     + monthMap.get(time).totalInTime + "分钟");
    136.             outView.setText("本月共拨打电话:" + monthMap.get(time).totalOutCall + "次\n总拨打时间:"
    137.                     + monthMap.get(time).totalOutTime + "分钟");
    138.             in.setText("本月最关心你的是:" + monthMap.get(time).mostInPeople + "\n共给你打过"
    139.                     + monthMap.get(time).mostInTimes + "次电话");
    140.             out.setText("本月你最关心的是:" + monthMap.get(time).mostOutPeople + "\n你共给他(她)打过"
    141.                     + monthMap.get(time).mostOutTimes + "次电话");
    142.             dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_info)
    143.                     .setTitle(time + " 通话时间统计").setView(view)
    144.                     .setPositiveButton("上个月", new DialogInterface.OnClickListener() {
    145.                         public void onClick(DialogInterface dialog, int which) {
    146.                             updateView(time, 0);

    147.                         }
    148.                     }).setNegativeButton("下个月", new DialogInterface.OnClickListener() {
    149.                         public void onClick(DialogInterface dialog, int which) {
    150.                             updateView(time, 1);
    151.                         }
    152.                     }).create();
    153.             dialog.setOnCancelListener(this);
    154.             dialog.show();
    155.         } else {
    156.             inView.setText("本月共接听电话:" + 0 + "次\n总接听时间:" + 0 + "分钟");
    157.             outView.setText("本月共拨打电话:" + 0 + "次\n总拨打时间:" + 0 + "分钟");
    158.             dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_info)
    159.                     .setTitle(time + "通话时间统计").setView(view)
    160.                     .setPositiveButton("上个月", new DialogInterface.OnClickListener() {
    161.                         public void onClick(DialogInterface dialog, int which) {
    162.                             updateView(time, 0);
    163.                         }
    164.                     }).setNegativeButton("下个月", new DialogInterface.OnClickListener() {
    165.                         public void onClick(DialogInterface dialog, int which) {
    166.                             updateView(time, 1);
    167.                         }
    168.                     }).create();
    169.             dialog.setOnCancelListener(this);
    170.             dialog.show();
    171.         }
    172.     }

    173.     private String getMost(Map<String, Integer> map) {
    174.         String mostName = "";
    175.         Set<String> totalName = map.keySet();
    176.         Integer most = 0, current=0;
    177.         for (String name : totalName) {
    178.             current = map.get(name);
    179.             if (current > most) {
    180.                 most = current;
    181.                 mostName = name;

    182.             }
    183.         }
    184.         return mostName;
    185.     }

    186.     class MonthTime {
    187.         long totalOutTime = 0;

    188.         int totalOutCall = 0;

    189.         long totalInTime = 0;

    190.         int totalInCall = 0;

    191.         String mostInPeople = "";

    192.         String mostOutPeople = "";

    193.         Integer mostInTimes = 0;

    194.         Integer mostOutTimes = 0;
    195.     }

    196.     private void updateView(String time, int mode) {
    197.         String year = time.substring(0, 4);
    198.         String month = time.substring(5, 7);
    199.         int iMonth = 0;
    200.         if (mode == 0) {
    201.             if (month.equals("01")) {
    202.                 year = String.valueOf(Integer.parseInt(year) - 1);
    203.                 month = "12";

    204.             } else {
    205.                 iMonth = Integer.parseInt(month) - 1;
    206.                 if (iMonth < 10) {
    207.                     month = "0" + iMonth;
    208.                 } else {
    209.                     month = String.valueOf(iMonth);
    210.                 }
    211.             }
    212.         } else {
    213.             if (month.equals("12")) {
    214.                 year = String.valueOf(Integer.parseInt(year) + 1);
    215.                 month = "01";

    216.             } else {
    217.                 iMonth = Integer.parseInt(month) + 1;
    218.                 if (iMonth < 10) {
    219.                     month = "0" + iMonth;
    220.                 } else {
    221.                     month = String.valueOf(iMonth);
    222.                 }
    223.             }
    224.         }
    225.         showDialog(year + "-" + month);
    226.         Log.d("test", month);
    227.         Log.d("test", year);
    228.     }

    229.     @Override
    230.     public void onCancel(DialogInterface dialog) {
    231.         finish();

    232.     }

    233. }
    要只显示一个Dialog,需要把背景的Activity设置为透明的就可以了,这个在manifest文件中进行设置:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.       package="org.test"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <uses-permission android:name="android.permission.READ_CONTACTS" />

  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">

  8.         <activity android:name=".TelTime"
  9.                   android:label="@string/app_name"
  10.                   android:theme="@android:style/Theme.Translucent">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>

  16.     </application>
  17. </manifest>
    对于Dialog的布局也很简单,只是采用了四个TextView。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.     xmlns:android=""
  4.     android:orientation="vertical"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent">
  7.     <TableLayout
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:shrinkColumns="1"
  11.         android:paddingTop="5dip">
  12.         <TableRow>
  13.             <TextView
  14.                 android:id="@+id/outtime"
  15.                 android:layout_width="wrap_content"
  16.                 android:layout_height="wrap_content"
  17.                 android:textSize="20dip"
  18.                 android:textColor="#fff00000" />
  19.         </TableRow>
  20.         <TableRow>
  21.             <TextView
  22.                 android:id="@+id/intime"
  23.                 android:layout_width="wrap_content"
  24.                 android:layout_height="wrap_content"
  25.                 android:textSize="20dip"
  26.                 android:textColor="#ff00ffff"/>
  27.         </TableRow>
  28.           <TableRow>
  29.             <TextView
  30.                 android:id="@+id/out"
  31.                 android:layout_width="wrap_content"
  32.                 android:layout_height="wrap_content"
  33.                 android:textSize="20dip"
  34.                 android:textColor="#fff0ff00" />
  35.         </TableRow>
  36.         <TableRow>
  37.             <TextView
  38.                 android:id="@+id/in"
  39.                 android:layout_width="wrap_content"
  40.                 android:layout_height="wrap_content"
  41.                 android:textSize="20dip"
  42.                 android:textColor="#ff00ff00"/>
  43.         </TableRow>
  44.     </TableLayout>
  45. </LinearLayout>

    最后,附上一个示例图片,如果有通话记录还会显示出你最关心的人和最关心你的人。



阅读(7455) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~