亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1009 | 回復: 0
打印 上一主題 下一主題

Android 統(tǒng)計通話時間的小軟件 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-12-21 08:41 |只看該作者 |倒序瀏覽
   現(xiàn)在很多人打電話都會使用一個套餐,比較劃算。通常一個月有多少分鐘免費呼叫,幾十到幾百條的免費短信。我自己寫了一個小軟件,可以統(tǒng)計一個月打出的電話總時間,可以幫助使用套餐的同學們把握好免費呼叫的時間。
   一般來講,android會將通話記錄都保存在一個特定的contentprovider中,用手機查看的通話記錄也是從里面查詢出來的數(shù)據(jù)。操作這個contentprovider的類就是CallLog類,它有一個內(nèi)部類Calls,在Calls中對uri等成員都已經(jīng)做好了定義,可以使用Calls的成員直接進行通話記錄的查詢。當然,對通話記錄的插入也是通過這個類來實現(xiàn)的。
   另外值得一提的是,如果在手機的通話記錄中將所有的通話記錄都刪除了話,是查不出任何數(shù)據(jù)的。
   程序的界面我采用了AlertDialog的形式,本來實現(xiàn)的功能和要顯示的內(nèi)容就很簡單,使用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 + " 通話時間統(tǒng)計").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 + "通話時間統(tǒng)計").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="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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>

    最后,附上一個示例圖片,如果有通話記錄還會顯示出你最關心的人和最關心你的人。



您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP