概述
当呼叫者属于黑名单列表 自动静音
[代码 步骤]
本着先易后难的原则 先介绍黑名单列表的制作:其会列出所有联系人列表 以CheckBox形式 可以添加/移除 黑名单
1. 定义所需布局:list.xml
Java代码
<?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>
<?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
Java代码
public void initial(){
initialView();
}
public void initialView(){
lView = (ListView)findViewById(R.id.list);
btnOK = (Button)findViewById(R.id.btnOK);
}
public void initial(){
initialView();
}
public void initialView(){
lView = (ListView)findViewById(R.id.list);
btnOK = (Button)findViewById(R.id.btnOK);
}
3. 定义ContactsAdapter 用于列出使用联系人
Java代码
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;
}
}
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 返回联系人的名字和号码
Java代码
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);
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 实例化
Java代码
adapter = new ContactsAdapter(this);
lView.setAdapter(adapter);
adapter = new ContactsAdapter(this);
lView.setAdapter(adapter);
4. 当按下btnOK 得到所有黑名单 并返回给前Activity:BlacklistMain
Java代码
btnOK.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadContactsChecked();
}
});
btnOK.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadContactsChecked();
}
});
5. loadContactsChecked 用于获取所有选中联系人列表 并返回之
Java代码
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();
}
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
Java代码
<?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>
<?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
Java代码
findViewById(R.id.buttonMain).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sendGo();
}
});
findViewById(R.id.buttonMain).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sendGo();
}
});
sendGo() 实现:
Java代码
public void sendGo(){
Intent i = new Intent(BlockMain.this,BlockManager.class);
this.startActivityForResult(i, ACTIVITY_CONTACTS_DO_CHECKED);
}
public void sendGo(){
Intent i = new Intent(BlockMain.this,BlockManager.class);
this.startActivityForResult(i, ACTIVITY_CONTACTS_DO_CHECKED);
}
其中 ACTIVITY_CONTACTS_DO_CHECKED 为int 用于标记具体是那个startActivity 之用
Java代码
public final static int ACTIVITY_CONTACTS_DO_CHECKED = 20;
public final static int ACTIVITY_CONTACTS_DO_CHECKED = 20;
* Activity 返回处理 即:从Intent中取出所有选中联系人列表 并存入List<String> 中
Java代码
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;
}
}
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() 即:根据不同电话状态做定制化
Java代码
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);
}
}
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() 用于判断来电号码是否在黑名单中 实现为:
Java代码
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 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;
}
对了 在最后别忘了监听电话状态 即:
Java代码
public void managerCallListener(){
aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
tManager = (TelephonyManager)getSystemService(
TELEPHONY_SERVICE);
cpListener = new CustomPhoneCallListener();
tManager.listen(cpListener, PhoneStateListener.
LISTEN_CALL_STATE);
}
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!!!
最后
以上就是时尚彩虹为你收集整理的让Android手机黑名单来电实现自动静音的全部内容,希望文章能够帮你解决让Android手机黑名单来电实现自动静音所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复