一入三冬唯盼夏
login
bean
package cn.itcast.loagin.bean;
import java.sql.Date;
public class Phone {
private int id;
private String phone;
private String classify;
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getClassify() {
return classify;
}
public void setClassify(String classify) {
this.classify = classify;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Phone(String phone, String classify, String date) {
super();
this.phone = phone;
this.classify = classify;
this.date = date;
}
public Phone(String phone, String date) {
super();
this.phone = phone;
this.date = date;
}
public Phone() {
}
}
dao
phonedao
package cn.itcast.loagin.db;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.itcast.loagin.bean.Phone;
public class PhoneDao {
private SQLiteHelper helper;
public PhoneDao(Context context) {
helper = new SQLiteHelper(context);
}
public void insert(Phone psw) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("phone", psw.getPhone());
values.put("classify", psw.getClassify());
values.put("date", psw.getDate());
db.insert("phone", null, values);
db.close();
}
public int update(Phone phone) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("phone", phone.getPhone());
values.put("classify", phone.getClassify());
int count = db.update("phone", values, "phone=?",
new String[] { phone.getPhone() });
db.close();
return count;
}
public List<Phone> queryAll() {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query("phone", new String[] { "phone", "classify" ,"date"},
null, null, null, null, null);
List<Phone> list = new ArrayList<Phone>();
while (c.moveToNext()) {
String phone = c.getString(c.getColumnIndex("phone"));
String classify = c.getString(c.getColumnIndex("classify"));
String date = c.getString(c.getColumnIndex("date"));
Phone number = new Phone(phone,classify,date);
list.add(number);
}
c.close();
db.close();
return list;
}
public String findData(String name, String data) {
String phone = "";
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query("phone", null, "phone=?", new String[] { name },
null, null, null);
while (c.moveToNext()) {
phone = c.getString(c.getColumnIndex(data));
}
c.close();
db.close();
return phone;
}
}
SQLiteHelper
package cn.itcast.loagin.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context) {
super(context, "CallRecords.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table phone (id integer primary key autoincrement,phone varchar(20),classify varchar(20),date varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
phoneState
CallReceiver
package cn.itcast.phoneState;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import cn.itcast.loagin.bean.Phone;
import cn.itcast.loagin.db.PhoneDao;
public class CallReceiver extends BroadcastReceiver {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static boolean flag = false;
/** 区分呼入呼出标记 */
private static boolean isOuting = false;
/** 当前操作电话号码 */
private static String currentNumber = null;
private String listenerNumber;
private PhoneDao dao;
@Override
public void onReceive(final Context context, Intent intent) {
dao = new PhoneDao(context);
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 呼出
isOuting = true;
String phonenumber = getResultData();// 被呼叫的电话号码
// 更新电话号码
if (null != phonenumber && !"".equals(phonenumber)) {
currentNumber = phonenumber;
}
} else {
if (!flag) {
TelephonyManager manager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
manager.listen(new PhoneStateListener() {
@SuppressLint({ "NewApi", "ShowToast" })
@Override
public void onCallStateChanged(int state,
String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
flag = true;
if (state == TelephonyManager.CALL_STATE_RINGING) {
// 来电状态
isOuting = false;
listenerNumber = incomingNumber;
Toast.makeText(context, "来电提醒: " + listenerNumber,
0).show();
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
if (isOuting) {
setData(context, currentNumber,"dialed","呼出电话: ");
} else {
setData(context, listenerNumber,"received","已接来电: ");
}
}
if (lastState == TelephonyManager.CALL_STATE_RINGING
&& state == TelephonyManager.CALL_STATE_IDLE) {
setData(context, incomingNumber,"missed","未接来电: ");
}
lastState = state;
}
private void setData(final Context context,
String incomingNumber,String classicy,String hint) {
Toast.makeText(context, hint + incomingNumber,
0).show();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
String date = formatter.format(curDate);
dao.insert(new Phone(incomingNumber, classicy, date));
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
}
MainActivity
package cn.itcast.phoneState;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.itcast.loagin.bean.Phone;
import cn.itcast.loagin.db.PhoneDao;
@SuppressLint({ "NewApi", "ResourceAsColor" })
public class MainActivity extends Activity implements OnClickListener {
private ListView listView;
private PhoneDao dao;
private MyAdapter adapter;
private Phone phoneNumber;
private List<Phone> dialed, received, missed, dataList;
private TextView tv_receivedCall, tv_missedCall, tv_dialedCalls;
private RelativeLayout rl_receivedCall, rl_missedCall, rl_dialedCalls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl_receivedCall = (RelativeLayout) findViewById(R.id.rl_receivedCall);
rl_missedCall = (RelativeLayout) findViewById(R.id.rl_missedCall);
rl_dialedCalls = (RelativeLayout) findViewById(R.id.rl_dialedCalls);
tv_receivedCall = (TextView) findViewById(R.id.tv_receivedCall);
tv_missedCall = (TextView) findViewById(R.id.tv_missedCall);
tv_dialedCalls = (TextView) findViewById(R.id.tv_dialedCalls);
rl_receivedCall.setOnClickListener(this);
rl_missedCall.setOnClickListener(this);
rl_dialedCalls.setOnClickListener(this);
listView = (ListView) findViewById(R.id.listview);
dao = new PhoneDao(this);
}
@Override
protected void onResume() {
super.onResume();
dataList = dao.queryAll();
dialed = new ArrayList<Phone>();
received = new ArrayList<Phone>();
missed = new ArrayList<Phone>();
if (dataList != null) {
for (Phone p : dataList) {
String classify = p.getClassify();
if ("received".equals(classify)) {
received.add(new Phone(p.getPhone(), p.getDate()));
} else if ("dialed".equals(classify)) {
dialed.add(new Phone(p.getPhone(), p.getDate()));
} else if ("missed".equals(classify)) {
missed.add(new Phone(p.getPhone(), p.getDate()));
}
}
}
adapter = new MyAdapter(dialed);
listView.setAdapter(adapter);
setTextColor(R.color.gray, R.color.gray, R.color.white);
}
private class MyAdapter extends BaseAdapter {
private List<Phone> list;
public MyAdapter(List<Phone> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
phoneNumber = list.get(position);
View item = convertView != null ? convertView : View.inflate(
MainActivity.this, R.layout.listview_item, null);
TextView tv_number = (TextView) item.findViewById(R.id.tv_number);
TextView tv_date = (TextView) item.findViewById(R.id.tv_date);
tv_number.setText("电话号码: " + phoneNumber.getPhone());
tv_date.setText(phoneNumber.getDate());
return item;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl_dialedCalls:
adapter = new MyAdapter(dialed);
listView.setAdapter(adapter);
setTextColor(R.color.gray, R.color.gray, R.color.white);
break;
case R.id.rl_receivedCall:
adapter = new MyAdapter(received);
listView.setAdapter(adapter);
setTextColor(R.color.white, R.color.gray, R.color.gray);
break;
case R.id.rl_missedCall:
adapter = new MyAdapter(missed);
listView.setAdapter(adapter);
setTextColor(R.color.gray, R.color.white, R.color.gray);
break;
}
}
private void setTextColor(int one, int two, int three) {
tv_receivedCall.setTextColor(getResources().getColor(one));
tv_missedCall.setTextColor(getResources().getColor(two));
tv_dialedCalls.setTextColor(getResources().getColor(three));
}
}
layout
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#212021"
android:orientation="horizontal"
android:weightSum="3" >
<RelativeLayout
android:id="@+id/rl_dialedCalls"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/tv_dialedCalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="已拨电话"
android:textColor="#5a5d5a" />
</RelativeLayout>
<View
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#000000" />
<RelativeLayout
android:id="@+id/rl_receivedCall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/tv_receivedCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="已接电话"
android:textColor="#5a5d5a" />
</RelativeLayout>
<View
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#000000" />
<RelativeLayout
android:id="@+id/rl_missedCall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/tv_missedCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="未接电话"
android:textColor="#5a5d5a" />
</RelativeLayout>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ll_title"
android:divider="#313431"
android:dividerHeight="1px" />
</RelativeLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="20dp"
android:text="电话号码"
android:textColor="#ffffff"
android:textSize="16dp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_number"
android:layout_below="@+id/tv_number"
android:layout_marginBottom="10dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="5dp"
android:text="时间"
android:textColor="#ffffff"
android:textSize="16dp" />
</RelativeLayout>
menu
activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"/>
</menu>
values
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="gray">#5a5d5a</color>
</resources>
string
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">电话状态</string>
<string name="menu_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">电话状态</string>
<string name="menu_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>