<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Caller.java
package com.demo.android.Caller;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class CallerActivity extends Activity {
private PhoneState phoneState;
static String TABLE_NAME="Caller";
static String ID="_id"; //使用SimpleCursorAdapter,欄位名稱必須為_id
static String PHONE="phone";
static String STATE="state";
static String[] COLUMNS = { ID, PHONE, STATE };
//android內建list樣式
//android.R.layout.simple_list_item_2
//android.R.id.hint
//android.R.id.text1
//android.R.id.text2
static int[] TO = { android.R.id.hint, android.R.id.text1, android.R.id.text2};
long selectedIndex = 0;//儲存點選ListView項目
DBConnection dbconn;
private Button InsBtn;
private Button DelBtn;
private RadioGroup stateRg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phoneState = new PhoneState(this);
TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telMgr.listen(phoneState, PhoneState.LISTEN_CALL_STATE);
//建立資料庫實體
dbconn=new DBConnection(this);
show();
InsBtn = (Button)findViewById(R.id.InsBtn);
DelBtn = (Button)findViewById(R.id.DelBtn);
stateRg = (RadioGroup) findViewById(R.id.stateRg);
ListView callerLv = (ListView)findViewById(R.id.callerLv);
//建立新增按鈕事件
InsBtn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//取得DatePicker設定的日期
String phoneEt = ((EditText)findViewById(R.id.phoneEt)).getText().toString();
String state = "";
//新增Note
switch(stateRg.getCheckedRadioButtonId()){
case R.id.normal:
state = "鈴聲";
break;
case R.id.vibrate:
state = "震動";
break;
case R.id.silent:
state = "無聲";
break;
}
insert( phoneEt, state);
//清除EditText
((EditText)findViewById(R.id.phoneEt)).setText("");
}
});
//建立刪除按鈕事件
DelBtn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
delete(selectedIndex);
}
});
callerLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
//儲存點選編號
selectedIndex = id;
}
});
}
//必須要覆寫OnResume,完成通話後才可以顯示資料表資料
@Override
protected void onResume() {
super.onResume();
show();
}
private void show() {
SQLiteDatabase db=dbconn.getWritableDatabase();
Cursor c=null;
//查詢資料 SELECT _id, date, note FROM Notes
c=db.query(TABLE_NAME, COLUMNS,null, null, null, null, null);
//使用SimpleCursorAdapter直接將資料表資料顯示至ListView上
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, COLUMNS, TO);
ListView callerLv = (ListView)findViewById(R.id.callerLv);
callerLv.setAdapter(adapter);
//把建立的Cursor實體交給Activity管理,
startManagingCursor(c);
db.close();
}
private void insert(String phone, String state) {
// 用ContentValues插入資料
ContentValues values=new ContentValues();
values.put(PHONE,phone);
values.put(STATE,state);
SQLiteDatabase db = dbconn.getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
//ListView顯示Caller表格內容
show();
}
private void delete(long id) {
// 刪除代號為id的資料
SQLiteDatabase db = dbconn.getWritableDatabase();
db.delete(TABLE_NAME, "_id=?" , new String[]{Long.toString(id)});
db.close();
//ListView顯示Caller表格內容
show();
}
}
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電話號碼" />
<EditText
android:id="@+id/phoneEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="phone">"
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="來電狀態" />
<RadioGroup
android:id="@+id/stateRg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="鈴聲" />
<RadioButton
android:id="@+id/vibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="震動" />
<RadioButton
android:id="@+id/silent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="無聲" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/InsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新增" />
<Button
android:id="@+id/DelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除" />
</LinearLayout>
<ListView
android:id="@+id/callerLv"
android:layout_width="match_parent"
android:layout_height="256dp"
android:layout_weight="0.56" >
</ListView>
</LinearLayout>
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
DBConnection.java
package com.demo.android.Caller;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBConnection extends SQLiteOpenHelper {
private static final String DATABASE_NAME="CallerDB";
private static final int DATABASE_VERSION=1;
public DBConnection(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//建立表格
//空格一定要保留
String sql = "CREATE TABLE Caller ("
+ " _id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " phone text not null, "
+ " state text not null" + ");";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
package com.demo.android.Caller;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneState extends PhoneStateListener {
private String stateStr;
private Context context;
private AudioManager audioManager;
public PhoneState(Context c){
super();
context = c;
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
}
@Override
public void onCallStateChanged(int state, String incomingNumber){
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
//待機
stateStr = "待機預設為震動模式";
//待機預設為震動模式
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//通話中
stateStr = "通話中";
break;
case TelephonyManager.CALL_STATE_RINGING:
//來電
stateStr = GetStat(incomingNumber);
if(stateStr.equals("鈴聲")){
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}else if(stateStr.equals("震動")){
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}else if(stateStr.equals("無聲")){
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
break;
}
Toast.makeText(context, stateStr, Toast.LENGTH_SHORT).show();
}
private String GetStat(String incomingNumber){
//建立資料庫實體
DBConnection dbconn = new DBConnection(context);
SQLiteDatabase db = dbconn.getReadableDatabase();
String state = "";
//取得所有資料放在list
Cursor c = db.query("Caller", new String[]{"state"}, "phone = ?", new String[]{incomingNumber}, null, null, null);
c.moveToFirst();
if( c.getCount() > 0 ){
state = c.getString(0);
}
c.close();
return state;
}
}
沒有留言:
張貼留言