现在很多人打电话都会使用一个套餐,比较划算。通常一个月有多少分钟免费呼叫,几十到几百条的免费短信。我自己写了一个小软件,可以统计一个月打出的电话总时间,可以帮助使用套餐的同学们把握好免费呼叫的时间。 一般来讲,android会将通话记录都保存在一个特定的contentprovider中,用手机查看的通话记录也是从里面查询出来的数据。操作这个contentprovider的类就是CallLog类,它有一个内部类Calls,在Calls中对uri等成员都已经做好了定义,可以使用Calls的成员直接进行通话记录的查询。当然,对通话记录的插入也是通过这个类来实现的。
另外值得一提的是,如果在手机的通话记录中将所有的通话记录都删除了话,是查不出任何数据的。
程序的界面我采用了AlertDialog的形式,本来实现的功能和要显示的内容就很简单,使用Activity的话感觉有点大。整个程序只有一个类TelTime.java,其实说起来很简单,就是读取出所有的通话记录,然后一条一条地按时间进行分类求和而已。
- package org.test;
-
-
import android.app.Activity;
-
import android.app.AlertDialog;
-
import android.content.ContentResolver;
-
import android.content.DialogInterface;
-
import android.database.Cursor;
-
import android.os.Bundle;
-
import android.provider.CallLog;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.TextView;
-
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
import java.util.HashMap;
-
import java.util.Map;
-
import java.util.Set;
-
-
public class TelTime extends Activity implements DialogInterface.OnCancelListener {
-
private TextView outView;
-
-
private TextView inView;
-
-
private TextView out;
-
-
private TextView in;
-
-
View view;
-
-
int type;
-
-
long callTime;
-
-
long totalOutTime = 0;
-
-
int totalOutCall = 0;
-
-
long totalInTime = 0;
-
-
int totalInCall = 0;
-
-
long singleTime = 0;
-
-
AlertDialog dialog;
-
-
Date date;
-
-
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
-
-
String nowTime;
-
-
String tagTime = "";
-
-
String time;
-
-
String name;
-
-
Map<String, MonthTime> monthMap = new HashMap<String, MonthTime>();
-
-
Map<String, Integer> mInMap = new HashMap<String, Integer>();
-
-
Map<String, Integer> mOutMap = new HashMap<String, Integer>();
-
-
MonthTime currentMonth;
-
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
// requestWindowFeature(Window.FEATURE_NO_TITLE);
-
nowTime = format.format((new Date()));
-
ContentResolver cr = getContentResolver();
-
final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[] {
-
CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.DURATION,
-
CallLog.Calls.CACHED_NAME
-
}, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
-
for (int i = 0; i < cursor.getCount(); i++) {
-
cursor.moveToPosition(i);
-
type = cursor.getInt(0);
-
callTime = cursor.getLong(2);
-
name = cursor.getString(3);
-
date = new Date(Long.parseLong(cursor.getString(1)));
-
time = format.format(date);
-
if (!time.equals(tagTime)) {
-
if (tagTime.equals("")) {
-
-
} else {
-
currentMonth.mostInPeople = getMost(mInMap);
-
currentMonth.mostInTimes = mInMap.get(currentMonth.mostInPeople);
-
currentMonth.mostOutPeople = getMost(mOutMap);
-
currentMonth.mostOutTimes = mOutMap.get(currentMonth.mostOutPeople);
-
monthMap.put(tagTime, currentMonth);
-
mInMap.clear();
-
mOutMap.clear();
-
}
-
currentMonth = new MonthTime();
-
tagTime = time;
-
// if (monthMap.containsKey(time)) {
-
// currentMonth = monthMap.get(time);
-
// monthMap.remove(time);
-
// } else {
-
// currentMonth = new MonthTime();
-
// }
-
}
-
if (callTime % 60 != 0) {
-
singleTime = callTime / 60 + 1;
-
} else {
-
singleTime = callTime / 60;
-
}
-
if (type == 1) {
-
currentMonth.totalInCall++;
-
currentMonth.totalInTime = currentMonth.totalInTime + singleTime;
-
if (mInMap.containsKey(name)) {
-
Integer time = mInMap.get(name);
-
mInMap.remove(name);
-
mInMap.put(name, time + 1);
-
} else {
-
if (name != null) {
-
mInMap.put(name, 1);
-
}
-
}
-
}
-
if (type == 2) {
-
currentMonth.totalOutCall++;
-
currentMonth.totalOutTime = currentMonth.totalOutTime + singleTime;
-
if (mOutMap.containsKey(name)) {
-
Integer time = mOutMap.get(name);
-
mOutMap.remove(name);
-
mOutMap.put(name, time + 1);
-
} else {
-
if (name != null) {
-
mOutMap.put(name, 1);
-
}
-
}
-
// Log.d("test", "name:" + name);
-
// Log.d("test", "time :" + tagTime);
-
}
-
if (type == 3) {
-
if (mInMap.containsKey(name)) {
-
Integer time = mInMap.get(name);
-
mInMap.remove(name);
-
mInMap.put(name, time + 1);
-
} else {
-
if (name != null) {
-
mInMap.put(name, 1);
-
}
-
}
-
}
-
-
-
}
-
-
showDialog(nowTime);
-
}
-
-
public void onStart() {
-
super.onStart();
-
-
}
-
-
private void showDialog(final String time) {
-
view = getLayoutInflater().inflate(R.layout.main, null);
-
-
outView = (TextView)view.findViewById(R.id.outtime);
-
inView = (TextView)view.findViewById(R.id.intime);
-
out = (TextView)view.findViewById(R.id.out);
-
in = (TextView)view.findViewById(R.id.in);
-
if (monthMap.containsKey(time)) {
-
inView.setText("本月共接听电话:" + monthMap.get(time).totalInCall + "次\n总接听时间:"
-
+ monthMap.get(time).totalInTime + "分钟");
-
outView.setText("本月共拨打电话:" + monthMap.get(time).totalOutCall + "次\n总拨打时间:"
-
+ monthMap.get(time).totalOutTime + "分钟");
-
in.setText("本月最关心你的是:" + monthMap.get(time).mostInPeople + "\n共给你打过"
-
+ monthMap.get(time).mostInTimes + "次电话");
-
out.setText("本月你最关心的是:" + monthMap.get(time).mostOutPeople + "\n你共给他(她)打过"
-
+ monthMap.get(time).mostOutTimes + "次电话");
-
dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_info)
-
.setTitle(time + " 通话时间统计").setView(view)
-
.setPositiveButton("上个月", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int which) {
-
updateView(time, 0);
-
-
}
-
}).setNegativeButton("下个月", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int which) {
-
updateView(time, 1);
-
}
-
}).create();
-
dialog.setOnCancelListener(this);
-
dialog.show();
-
} else {
-
inView.setText("本月共接听电话:" + 0 + "次\n总接听时间:" + 0 + "分钟");
-
outView.setText("本月共拨打电话:" + 0 + "次\n总拨打时间:" + 0 + "分钟");
-
dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_info)
-
.setTitle(time + "通话时间统计").setView(view)
-
.setPositiveButton("上个月", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int which) {
-
updateView(time, 0);
-
}
-
}).setNegativeButton("下个月", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int which) {
-
updateView(time, 1);
-
}
-
}).create();
-
dialog.setOnCancelListener(this);
-
dialog.show();
-
}
-
}
-
-
private String getMost(Map<String, Integer> map) {
-
String mostName = "";
-
Set<String> totalName = map.keySet();
-
Integer most = 0, current=0;
-
for (String name : totalName) {
-
current = map.get(name);
-
if (current > most) {
-
most = current;
-
mostName = name;
-
-
}
-
}
-
return mostName;
-
}
-
-
class MonthTime {
-
long totalOutTime = 0;
-
-
int totalOutCall = 0;
-
-
long totalInTime = 0;
-
-
int totalInCall = 0;
-
-
String mostInPeople = "";
-
-
String mostOutPeople = "";
-
-
Integer mostInTimes = 0;
-
-
Integer mostOutTimes = 0;
-
}
-
-
private void updateView(String time, int mode) {
-
String year = time.substring(0, 4);
-
String month = time.substring(5, 7);
-
int iMonth = 0;
-
if (mode == 0) {
-
if (month.equals("01")) {
-
year = String.valueOf(Integer.parseInt(year) - 1);
-
month = "12";
-
-
} else {
-
iMonth = Integer.parseInt(month) - 1;
-
if (iMonth < 10) {
-
month = "0" + iMonth;
-
} else {
-
month = String.valueOf(iMonth);
-
}
-
}
-
} else {
-
if (month.equals("12")) {
-
year = String.valueOf(Integer.parseInt(year) + 1);
-
month = "01";
-
-
} else {
-
iMonth = Integer.parseInt(month) + 1;
-
if (iMonth < 10) {
-
month = "0" + iMonth;
-
} else {
-
month = String.valueOf(iMonth);
-
}
-
}
-
}
-
showDialog(year + "-" + month);
-
Log.d("test", month);
-
Log.d("test", year);
-
}
-
-
@Override
-
public void onCancel(DialogInterface dialog) {
-
finish();
-
-
}
-
-
}
要只显示一个Dialog,需要把背景的Activity设置为透明的就可以了,这个在manifest文件中进行设置:
- <?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="org.test"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<uses-permission android:name="android.permission.READ_CONTACTS" />
-
-
<application android:icon="@drawable/icon" android:label="@string/app_name">
-
-
<activity android:name=".TelTime"
-
android:label="@string/app_name"
-
android:theme="@android:style/Theme.Translucent">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
-
</application>
-
</manifest>
对于Dialog的布局也很简单,只是采用了四个TextView。
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout
-
xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
<TableLayout
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:shrinkColumns="1"
-
android:paddingTop="5dip">
-
<TableRow>
-
<TextView
-
android:id="@+id/outtime"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="20dip"
-
android:textColor="#fff00000" />
-
</TableRow>
-
<TableRow>
-
<TextView
-
android:id="@+id/intime"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="20dip"
-
android:textColor="#ff00ffff"/>
-
</TableRow>
-
<TableRow>
-
<TextView
-
android:id="@+id/out"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="20dip"
-
android:textColor="#fff0ff00" />
-
</TableRow>
-
<TableRow>
-
<TextView
-
android:id="@+id/in"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="20dip"
-
android:textColor="#ff00ff00"/>
-
</TableRow>
-
</TableLayout>
-
</LinearLayout>
最后,附上一个示例图片,如果有通话记录还会显示出你最关心的人和最关心你的人。
阅读(7455) | 评论(0) | 转发(0) |