论坛首页 移动开发技术论坛

黑名单 自动静音

浏览 5573 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-16  

黑名单 自动静音

 

当呼叫者属于黑名单列表 自动静音

 

 

[代码 步骤]

本着先易后难的原则 先介绍黑名单列表的制作:其会列出所有联系人列表 以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!!!

 

 

 

   发表时间:2010-05-08  
楼主  养成把textview的text写到string哈   不符合代码规范  嘻嘻
0 请登录后投票
   发表时间:2010-05-08  
yzhong_sa 写道
楼主  养成把textview的text写到string哈   不符合代码规范  嘻嘻

因为这仅仅为demo 那样写要文件不断切换 比较麻烦 所以这样写 简便点~~~~
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics