黑名單 自動靜音
當呼叫者屬於黑名單列表 自動靜音
[代碼 步驟]
本着先易後難的原則 先介紹黑名單列表的製作:其會列出所有聯繫人列表 以CheckBox形式 可以添加/移除 黑名單
1. 定義所需佈局:list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnOK"
android:layout_gravity="right"
android:text="OK"
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
2. 初始化View
public void initial(){
initialView();
}
public void initialView(){
lView = (ListView)findViewById(R.id.list);
btnOK = (Button)findViewById(R.id.btnOK);
}
3. 定義ContactsAdapter 用於列出使用聯繫人
public class ContactsAdapter extends BaseAdapter {
Activity activity;
public ContactsAdapter(Activity a){
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
CheckBox rb = new CheckBox(activity);
rb.setText(" "+getNameById(arg0)+" |"+getNumberById(arg0));
return rb;
}
}
其中 getNameById() getNumberById() 可以根據position 返回聯繫人的名字和號碼
public String getNameById(int id){
cursor.moveToPosition(id);
int index = cursor.getColumnIndex(People.NAME);
return cursor.getString(index);
}
public String getNumberById(int id){
cursor.moveToPosition(id);
int index = cursor.getColumnIndex(People.NUMBER);
return cursor.getString(index);
}
其中 cursor = getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
3. ContactsAdapter 實例化
adapter = new ContactsAdapter(this);
lView.setAdapter(adapter);
4. 當按下btnOK 得到所有黑名單 並返回給前Activity:BlacklistMain
btnOK.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadContactsChecked();
}
});
5. loadContactsChecked 用於獲取所有選中聯繫人列表 並返回之
public void loadContactsChecked(){
List<String> list = new ArrayList<String>();
for(int i=0;i<lView.getCount();i++){
CheckBox rButton = (CheckBox)lView.getChildAt(i);
if(rButton.isChecked()){
list.add(getNumberById(i));
}
}
sendBack(list);
}
public void sendBack(List<String> l){
Intent intent = new Intent();
Bundle bundle = new Bundle();
String[] sArray = new String[l.size()];
l.toArray(sArray);
bundle.putStringArray("phone", sArray);
intent.putExtras(bundle);
this.setResult(RESULT_OK, intent);
this.finish();
}
至此 黑名單列表選取 已經完成 下面講自動靜音功能 文件爲:BlacklistMain
佈局:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/buttonMain"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="黑名單"
/>
<Button
android:id="@+id/buttonClear"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清屏"
/>
<Button
android:id="@+id/buttonList"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列舉"
/>
</LinearLayout>
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
1. 與BlacklistManager 即:黑名單列表選取 連接 的代碼
* 單擊buttonMain 跳轉至BlacklistManager
findViewById(R.id.buttonMain).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sendGo();
}
});
sendGo() 實現:
public void sendGo(){
Intent i = new Intent(BlockMain.this,BlockManager.class);
this.startActivityForResult(i, ACTIVITY_CONTACTS_DO_CHECKED);
}
其中 ACTIVITY_CONTACTS_DO_CHECKED 爲int 用於標記具體是那個startActivity 之用
public final static int ACTIVITY_CONTACTS_DO_CHECKED = 20;
* Activity 返回處理 即:從Intent中取出所有選中聯繫人列表 並存入List<String> 中
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
switch(requestCode){
case ACTIVITY_CONTACTS_DO_CHECKED:
Bundle b = data.getExtras();
String[] s = b.getStringArray("phone");
blockList.clear();
for(String i:s){
blockList.add(i);
}
showBloclist();
break;
}
}
下面主要說下 電話呼叫攔截且靜音 代碼
1. 繼承PhoneStateListener 並實現其中的onCallStateChanged() 即:根據不同電話狀態做定製化
public class CustomPhoneCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
textStatus.append("\n"+"status:CALL_STATE_IDLE");
aManager.setRingerMode(AudioManager.
RINGER_MODE_NORMAL);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
textStatus.append("\n"+"status:CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
textStatus.append("\n"+"status:CALL_STATE_RINGING:"+incomingNumber);
showBloclist();
if(isBlock(incomingNumber)){
aManager.setRingerMode(AudioManager.
RINGER_MODE_SILENT);
showToast("Call-number:"+incomingNumber+"|silent");
}
else {
showToast("Call-number:"+incomingNumber);
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
其中 isBlock() 用於判斷來電號碼是否在黑名單中 實現爲:
public boolean isBlock(String s){
//to load all String stored in List<String> to String[]
String[] bArray = new String[blockList.size()];
blockList.toArray(bArray);
for(String s1:bArray){
if(s1.replace("-", "").equals(s)){
return true;
}
}
return false;
}
對了 在最後別忘了監聽電話狀態 即:
public void managerCallListener(){
aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
tManager = (TelephonyManager)getSystemService(
TELEPHONY_SERVICE);
cpListener = new CustomPhoneCallListener();
tManager.listen(cpListener, PhoneStateListener.
LISTEN_CALL_STATE);
}
3. emulator 運行截圖:
* 添加黑名單 選取:137 128
* 以號碼:137 呼叫該emulator 即:gsm call 137
最後 BS android ! 不知道什麼原因 開始在emulator 老失敗 後來發現其不能打/接聽電話 一撥出電話 就提示: Not register on network. 沒辦法 只有重新創建emulator再試
至於怎麼以指導號碼向該emulator打電話 以前的博客有 大家自己找一下吧!
done!!!