Android開發之黑名單來電自動掛斷
本實例允許用戶動態添加號碼到黑名單,并實現黑名單來電自動掛斷。程序通過創建PhoneStateListener監聽器來監聽TelephonyManager的通話狀態來實現該功能。
由于自Android 10之后Android不再對外公開掛斷電話的API,如果需要掛斷電話必須使用AIDL與電話管理Service進行通信,并調用服務中的API實現結束電話。
為了調用遠程的AIDL Service,開發者需要將Android源碼中的如下兩個文拷到指定位置:
com.android.internal.telephony包下的ITelephony.aidl
android.telephony包下的NeighboringCellInfo.aidl
這樣就會在gen文件夾下生成相應的.Java文件。
另外調用API的方法如果有不解的地方可以參考:Class.forName()的作用與使用總結
另外本實例使用BaseAdapter作為ListView的適配器,BaseAdapter具有很強的定制型。
演示圖片:
程序代碼: package com.jph.callguard;
import java.lang.reflect.Method;
import java.util.ArrayList;
import com.android.internal.telephony.ITelephony;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
/**
* Describe:</br>
* 黑名單自動掛斷
* 本實例實現了獲取聯系人,并將選中的聯系人添加到黑名單中
* 并通過ITelephony.aidl與NeighboringCellInfo.aidl
* 接口使用AIDL與電話管理Servic進行通信掛斷黑名單中的電話
* @author jph
* Time:2014.07.24
* */
public class CallGuard extends Activity {
Button btnManage;
TelephonyManager tManager;
//創建一個集合用于保存黑名單中的號碼
ArrayList<String>blockList=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnManage=(Button)findViewById(R.id.btnManage);
tManager=(TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//創建電話狀態監聽器
PhoneStateListener pStateListener=new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
switch (state) {
case TelephonyManager.CALL_STATE_IDLE://空閑狀態不做處理
break;
case TelephonyManager.CALL_STATE_OFFHOOK://接起電話不做處理
break;
case TelephonyManager.CALL_STATE_RINGING://正在響鈴
//如果來電號碼在黑名單中則自動掛斷
if (isBlock(incomingNumber)) {
try {
//獲取android.os.ServiceManager類的對象的getService()方法
Method method=Class.forName("android.os.ServiceManager").
getMethod("getService",String.class);
// 獲取遠程TELEPHONY_SERVICE的IBinder對象的代理
IBinder binder =(IBinder)method.invoke(null, new Object[] {TELEPHONY_SERVICE});
// 將IBinder對象的代理轉換為ITelephony對象
ITelephony telephony=ITelephony.Stub.asInterface(binder);
//掛斷電話
telephony.endCall();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
//為TelephonyManager添加監聽器
tManager.listen(pStateListener, PhoneStateListener.LISTEN_CALL_STATE);
btnManage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//查詢通信錄中的電話號碼
final Cursor cursor=getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
//創建一個BaseAdapter作為ListView的適配器
BaseAdapter adapter=new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//將游標的指針移動到指定位置
cursor.moveToPosition(position);
CheckBox box=new CheckBox(CallGuard.this);
//獲取指定位置的電話號碼
String number=cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
//去掉電話號碼中間的中劃線、空格
number.replace("-", "").replace(" ", "");
box.setText(number);
//如果該號碼已經被加入黑名單、默認勾選該號碼
if (isBlock(number)) {
box.setChecked(true);
}
return box;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
//返回列表的總條數
@Override
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
};
//加載line布局文件
View view=getLayoutInflater().inflate(R.layout.line, null);
//獲取line布局文件中Id為list的組件
final ListView list=(ListView)view.findViewById(R.id.list);
list.setAdapter(adapter);
new AlertDialog.Builder(CallGuard.this).setView(view).setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
blockList.clear();
for (int i = 0; i < list.getCount(); i++) {
CheckBox box=(CheckBox)list.getChildAt(i);
//如果聯系人被選中則將其添加到blockList集合
if (box.isChecked()) {
blockList.add(box.getText().toString());
}
}
}
}).show();
}
});
}
private boolean isBlock(String number) {
// TODO Auto-generated method stub
//判斷號碼是否在blockList集合中
for (String s:blockList) {
if (s.equals(number)) {
return true;
}
}
return false;
}
} </pre></span><span style="color:#ff0000;font-size:24px;">最后別忘添加相應的權限:</span> <p></p>
AndroidManifest.xml
<!-- 授予該應用控制通話的權限 --> <uses-permission android:name="android.permission.CALL_PHONE" /> <!-- 授予該應用讀取通話狀態的權限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 授予讀聯系人ContentProvider的權限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 授予寫聯系人ContentProvider的權限 --> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!